改造 H5 第三方地图导航
Some checks failed
CI / verify (push) Has been cancelled

This commit is contained in:
lyf
2026-07-13 16:56:18 +08:00
parent 9c003c16dc
commit 140f13633e
5 changed files with 308 additions and 220 deletions

View File

@@ -1,6 +1,6 @@
/**
* 地图导航调起服务
* 使用 uni.openLocation 调起设备地图 App 进行导航
* 使用原生地图 Scheme 调起设备地图 App 进行导航
*/
import { MUSEUM_LOCATION } from '@/config/museum'
@@ -18,6 +18,62 @@ export interface NavigationOptions {
scale?: number
}
type NavigationTarget = Pick<NavigationOptions, 'latitude' | 'longitude' | 'name'>
export type MobilePlatform = 'ios' | 'android'
const encode = (value: string) => encodeURIComponent(value)
export function buildBaiduNavigationUrl(target: NavigationTarget): string {
const destination = encode(
`latlng:${target.latitude},${target.longitude}|name:${target.name}`
)
return `baidumap://map/direction?destination=${destination}&coord_type=gcj02&mode=driving&src=museum-guide`
}
export function buildAmapNavigationUrl(
target: NavigationTarget,
platform: MobilePlatform
): string {
const scheme = platform === 'ios' ? 'iosamap://path' : 'androidamap://route'
return `${scheme}?sourceApplication=${encode('深圳自然博物馆')}&dlat=${target.latitude}&dlon=${target.longitude}&dname=${encode(target.name)}&dev=0&t=0`
}
const detectMobilePlatform = (): MobilePlatform => {
if (typeof window === 'undefined') {
return 'android'
}
const { userAgent, platform, maxTouchPoints } = window.navigator
const isIos = /iPhone|iPad|iPod/i.test(userAgent)
|| (platform === 'MacIntel' && maxTouchPoints > 1)
return isIos ? 'ios' : 'android'
}
const openNativeMapScheme = (url: string): boolean => {
if (typeof window === 'undefined' || !window.location) {
uni.showToast({
title: '请在 H5 浏览器中打开地图',
icon: 'none'
})
return false
}
try {
window.location.href = url
return true
} catch (error) {
console.error('调起地图应用失败:', error)
uni.showToast({
title: '无法调起地图应用',
icon: 'none'
})
return false
}
}
/**
* 调起外部地图应用进行导航
* @param options 导航选项
@@ -25,42 +81,27 @@ export interface NavigationOptions {
*/
export function openExternalNavigation(options: NavigationOptions): Promise<boolean> {
return new Promise((resolve) => {
const scale = options.scale ?? 18
uni.showActionSheet({
itemList: ['百度地图', '高德地图'],
success: ({ tapIndex }) => {
const url = tapIndex === 0
? buildBaiduNavigationUrl(options)
: buildAmapNavigationUrl(options, detectMobilePlatform())
uni.openLocation({
latitude: options.latitude,
longitude: options.longitude,
name: options.name,
address: options.address ?? '',
scale,
success: () => {
console.log('成功调起地图导航:', options.name)
resolve(true)
resolve(openNativeMapScheme(url))
},
fail: (err) => {
console.error('调起地图导航失败:', err)
// 根据错误类型提供友好的提示
if (err.errMsg) {
if (err.errMsg.includes('cancel')) {
// 用户取消
resolve(false)
} else if (err.errMsg.includes('auth deny') || err.errMsg.includes('auth reject')) {
uni.showToast({
title: '需要位置权限才能导航',
icon: 'none'
})
resolve(false)
} else {
uni.showToast({
title: '无法调起地图应用',
icon: 'none'
})
resolve(false)
}
} else {
if (err.errMsg?.includes('cancel')) {
resolve(false)
return
}
console.error('打开地图选择失败:', err)
uni.showToast({
title: '无法打开地图选择',
icon: 'none'
})
resolve(false)
}
})
})