72 lines
2.1 KiB
JavaScript
72 lines
2.1 KiB
JavaScript
/**
|
|
* 打开导航 APP
|
|
* @param {number} lat - 纬度
|
|
* @param {number} lng - 经度
|
|
* @param {string} name - 地点名称
|
|
*/
|
|
export function openNavigation(lat, lng, name) {
|
|
// #ifdef APP-PLUS
|
|
const apps = []
|
|
|
|
// 检测高德地图
|
|
if (plus.runtime.isApplicationExist({ pname: 'com.autonavi.minimap', action: '' })) {
|
|
apps.push({
|
|
title: '高德地图',
|
|
scheme: `androidamap://navi?sourceApplication=odf&lat=${lat}&lon=${lng}&dev=0&style=2`
|
|
})
|
|
}
|
|
|
|
// 检测百度地图
|
|
if (plus.runtime.isApplicationExist({ pname: 'com.baidu.BaiduMap', action: '' })) {
|
|
apps.push({
|
|
title: '百度地图',
|
|
scheme: `baidumap://map/navi?location=${lat},${lng}&src=odf&coord_type=gcj02`
|
|
})
|
|
}
|
|
|
|
// 检测腾讯地图
|
|
if (plus.runtime.isApplicationExist({ pname: 'com.tencent.map', action: '' })) {
|
|
apps.push({
|
|
title: '腾讯地图',
|
|
scheme: `qqmap://map/routeplan?type=drive&to=${encodeURIComponent(name)}&tocoord=${lat},${lng}&referer=odf`
|
|
})
|
|
}
|
|
|
|
if (apps.length === 0) {
|
|
uni.showToast({ title: '未检测到导航应用', icon: 'none' })
|
|
return
|
|
}
|
|
|
|
uni.showActionSheet({
|
|
itemList: apps.map(a => a.title),
|
|
success(res) {
|
|
const selected = apps[res.tapIndex]
|
|
plus.runtime.openURL(selected.scheme, (err) => {
|
|
uni.showToast({ title: '打开导航失败', icon: 'none' })
|
|
})
|
|
}
|
|
})
|
|
// #endif
|
|
|
|
// #ifdef H5
|
|
// 手机浏览器中尝试通过 scheme 唤起高德地图 APP
|
|
const schemeUrl = `amapuri://route/plan/?dlat=${lat}&dlon=${lng}&dname=${encodeURIComponent(name)}&dev=0&t=0`
|
|
const webUrl = `https://uri.amap.com/navigation?to=${lng},${lat},${encodeURIComponent(name)}&mode=car&src=odf`
|
|
|
|
// 先尝试唤起 APP
|
|
const startTime = Date.now()
|
|
const iframe = document.createElement('iframe')
|
|
iframe.style.display = 'none'
|
|
iframe.src = schemeUrl
|
|
document.body.appendChild(iframe)
|
|
|
|
setTimeout(() => {
|
|
document.body.removeChild(iframe)
|
|
// 如果 2 秒内没有离开页面,说明 APP 没唤起,用 Web 版(新窗口打开)
|
|
if (Date.now() - startTime < 2500) {
|
|
window.open(webUrl, '_blank')
|
|
}
|
|
}, 2000)
|
|
// #endif
|
|
}
|