cordova:跳转第三方地图APP

跳转第三方地图APP

本文章记录2020.08.10~2020.08.11给APP增加地图跳转踩过的坑

参考1:https://blog.csdn.net/padding_scater/article/details/78997220
参考2:高德地图: https://lbs.amap.com/api/amap-mobile/guide/android/route
参考3:百度地图: http://lbsyun.baidu.com/index.php?title=uri/api/android
参考4:腾讯地图: https://lbs.qq.com/webApi/uriV1/uriGuide/uriMobileRoute



1:安装所需的插件,插件使用说明参考https://www.npmjs.com/

(1)使用内置浏览器:cordova plugin add cordova-plugin-inappbrowser
(2)判断是IOS还是android:cordova plugin add cordova-plugin-device
(3)检查第三方APP是否存在:cordova plugin add cordova-plugin-appavailability
(4)跳转第三方地图APP:cordova plugin add cordovapluginaddcom.lampa.startapp


2:修改config.xml


  //高德地图 
  //百度地图 
  //腾讯地图
……


3:代码实现

(1)判断ios还是android

var u=navigator.userAgent;
var isAndroid=u.indexOf('Android')>-1||u.indexOf('Adr')>-1;//android终端
var isiOS=!!u.match(/\(i[^;]+;(U;)?CPU.+MacOSX/);//ios终端

(2)检测手机是否安装该地图APP,以及跳转对应APP(格式不会整理,贴到vscode里格式化一下吧)

//URL调起地图APP
jumpMap(schemeIntent, jumpuri){
  appAvailability.check(schemeIntent, hasAppPackage, notAppPackage);
 //存在对应APP function hasAppPackage() {
var sApp;
 if (isIOS) { //苹果手机
 sApp = startApp.set(jumpuri); }
else { //安卓手机
 sApp = startApp.set({ action: "ACTION_VIEW", category: "CATEGORY_DEFAULT", type: "text/css", package: schemeIntent, uri: jumpuri, flags: ["FLAG_ACTIVITY_CLEAR_TOP", "FLAG_ACTIVITY_CLEAR_TASK"], intentstart: "startActivity", }); }
 sApp.start( function () { },//跳转成功
 function (error) { } //失败
 );  }
 //不存在对应APP
 function notAppPackage() { console.log("====不存在对应APP"); }},

(3)拼接地图url

百度地图:

苹果端:schemeIntent="baidumap://";
jumpuri = "baidumap://map/marker?location=40.047669,116.313082&title=我的位置&content=百度奎科大厦&src=ios.baidu.openAPIdemo";
安卓端:schemeIntent = "com.baidu.BaiduMap";
jumpuri = "baidumap://map/direction?origin=name:对外经贸大学|latlng:39.98871,116.43234&destination=西直门&coord_type=bd09ll&mode=transit&sy=3&index=0&target=1&src=andr.baidu.openAPIdemo"

高德地图:

苹果端:schemeIntent = "iosamap://";
  jumpuri = "iosamap://path?sourceApplication=applicationName&sid=&slat=39.92848272&slon=116.39560823&sname=A&did=&dlat=39.98848272&dlon=116.47560823&dname=B&dev=0&t=0";
安卓端:schemeIntent = "com.autonavi.minimap";
  jumpuri = "amapuri://route/plan/?sid=&slat=39.92848272&slon=116.39560823&sname=A&did=&dlat=39.98848272&dlon=116.47560823&dname=B&dev=0&t=0";

腾讯地图:(苹果端的有效url我没找到,官网写ios和Android一样的,但我就是跳不过去……)

安卓端:schemeIntent = "com.tencent.map";
jumpuri = "qqmap://map/routeplan?type=drive&from=清华&fromcoord=39.994745,116.247282&to=怡和世家&tocoord=39.867192,116.493187&referer=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77"

你可能感兴趣的:(cordova:跳转第三方地图APP)