H5通过Scheme唤起高德地图、百度地图APP,路线规划
代码如下:
HTML代码
1 2 3 4 5 6 7 8 9
| <p class="f-tac"> <a id="gaode"> 高德路线规划 </a> </p> <p class="f-tac"> <a id="baidu"> 百度路线规划 </a> </p> <p class="f-tac"> (提示:不支持在微信中打开路线规划) </p>
|
JS代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
const getMapScheme = (_from: any, to: any, mapType = "gaode") => { const u = navigator.userAgent; const isAndroid = u.indexOf("Android") > -1 || u.indexOf("Adr") > -1;
const andriodBaidu = (to: any) => { return `bdapp://map/direction?destination=name:${to.name}|latlng:${to.latitude},${to.longitude}&coord_type=gcj02&mode=driving&src=andr.jianghu.jianhao`; };
const iOSBaidu = (to: any) => { return `baidumap://map/direction?destination=name:${to.name}|latlng:${to.latitude},${to.longitude}&coord_type=gcj02&mode=driving&src=ios.jianghu.jianhao`; };
const andriodGaode = (to: any) => { return `amapuri://route/plan/?sourceApplication=mhc&dlat=${to.latitude}&dlon=${to.longitude}&dname=${to.name}&dev=0&t=0`; };
const iOSGaode = (to: any) => { return `iosamap://path?sourceApplication=mhc&dlat=${to.latitude}&dlon=${to.longitude}&dname=${to.name}&dev=0&t=0`; };
if (mapType == "baidu") { if (isAndroid) { return andriodBaidu(to); } else { return iOSBaidu(to); } } else if (mapType == "gaode") { if (isAndroid) { return andriodGaode(to); } else { return iOSGaode(to); } } };
const from = {}; const to = { name: "武汉长江大桥", longitude: 114.29, latitude: 30.55 };
document .querySelector("#gaode") ?.setAttribute("href", getMapScheme(from, to, "gaode") as string); document .querySelector("#baidu") ?.setAttribute("href", getMapScheme(from, to, "baidu") as string);
|