前言:最近做一款外賣配送的應(yīng)用玫膀,需要直接打開第三方地圖,簡(jiǎn)單記錄下
- 需要條件編譯哦,只有app端可以呀八回,小程序不支持哦
- 小程序的打開第三方地圖需要依托于uni.openLocation,不過(guò)這樣會(huì)先打開地圖頁(yè),然后右下角會(huì)有選擇打開第三方圖片的按鈕
//打開第三方地圖
export function openMap(latitude,longitude,name){
let url = "";
if (plus.os.name == "Android") {
let hasBaiduMap = plus.runtime.isApplicationExist({
pname: 'com.baidu.BaiduMap',
action: 'baidumap://'
});
let hasAmap = plus.runtime.isApplicationExist({
pname: 'com.autonavi.minimap',
action: 'androidamap://'
});
let urlBaiduMap = `baidumap://map/marker?location=${latitude},${longitude}&title=${name}&coord_type=gcj02&src=andr.baidu.openAPIdemo`
urlBaiduMap = encodeURI(urlBaiduMap)
let urlAmap = `androidamap://viewMap?sourceApplication=appname&poiname=${name}&lat=${latitude}&lon=${longitude}&dev=0`
urlAmap = encodeURI(urlAmap)
if (hasAmap && hasBaiduMap) {
plus.nativeUI.actionSheet({
title: "選擇地圖應(yīng)用",
cancel: "取消",
buttons: [{
title: "百度地圖"
}, {
title: "高德地圖"
}]
}, function(e) {
switch (e.index) {
case 1:
plus.runtime.openURL(urlBaiduMap);
break;
case 2:
plus.runtime.openURL(urlAmap);
break;
}
})
} else if (hasAmap) {
plus.runtime.openURL(urlAmap);
} else if (hasBaiduMap) {
plus.runtime.openURL(urlBaiduMap);
} else {
plus.nativeUI.alert("本機(jī)未安裝指定的地圖應(yīng)用");
// url = "geo:"+latitude+","+longitude+"?q=%e6%95%b0%e5%ad%97%e5%a4%a9%e5%a0%82";
// plus.runtime.openURL(url); //如果是國(guó)外應(yīng)用缠诅,應(yīng)該優(yōu)先使用這個(gè)溶浴,會(huì)啟動(dòng)google地圖。這個(gè)接口不能統(tǒng)一坐標(biāo)系管引,進(jìn)入百度地圖時(shí)會(huì)有偏差
}
} else {
// iOS上獲取本機(jī)是否安裝了百度高德地圖士败,需要在manifest里配置,在manifest.json文件app-plus->distribute->apple->urlschemewhitelist節(jié)點(diǎn)下添加(如urlschemewhitelist:["iosamap","baidumap"])
plus.nativeUI.actionSheet({
title: "選擇地圖應(yīng)用",
cancel: "取消",
buttons: [{
title: "Apple地圖"
}, {
title: "百度地圖"
}, {
title: "高德地圖"
}]
}, function(e) {
console.log("e.index: " + e.index);
switch (e.index) {
case 1:
url = `http://maps.apple.com/?q=%e6%95%b0%e5%ad%97%e5%a4%a9%e5%a0%82&ll=${latitude},${longitude}&spn=0.008766,0.019441`;
break;
case 2:
url = `baidumap://map/marker?location=${latitude},${longitude}&title=${name}&content=${name}&src=ios.baidu.openAPIdemo&coord_type=gcj02`;
break;
case 3:
url = `iosamap://viewMap?sourceApplication=applicationName&poiname=${name}&lat=${latitude}&lon=${longitude}&dev=0`
break;
default:
break;
}
if (url != "") {
url = encodeURI(url)
plus.runtime.openURL(url, function(e) {
plus.nativeUI.alert("本機(jī)未安裝指定的地圖應(yīng)用");
});
}
})
}
}