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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
function getSystemInfo() { return new Promise((resolve) => { uni.getSystemInfo({ success: (res) => { resolve(res); }, fail: () => { resolve({}); } }); }); }
function checkAppInstalled(pname, action) { try { return plus.runtime.isApplicationExist({ pname: pname || '', action: action || '' }); } catch (error) { console.error('检测应用安装失败:', error); return false; } }
async function getAllMaps(latitude, longitude, name = '') { const systemInfo = await getSystemInfo(); const platform = systemInfo.platform || ''; const isIOS = platform === 'ios'; const isAndroid = platform === 'android'; const maps = []; maps.push({ name: '高德地图', pname: 'com.autonavi.minimap', action: 'iosamap://', scheme: isIOS ? 'iosamap://' : 'androidamap://', url: isIOS ? `iosamap://path?sourceApplication=applicationName&dname=${encodeURIComponent(name || '目的地')}&dlat=${latitude}&dlon=${longitude}&dev=0&t=0` : `androidamap://route?sourceApplication=amap&dname=${encodeURIComponent(name || '目的地')}&dlat=${latitude}&dlon=${longitude}&dev=0&t=0` }); maps.push({ name: '百度地图', pname: 'com.baidu.BaiduMap', action: 'baidumap://', scheme: 'baidumap://', url: `baidumap://map/direction?origin=我的位置&destination=latlng:${latitude},${longitude}|name:${encodeURIComponent(name || '目的地')}&mode=driving&coord_type=gcj02` }); maps.push({ name: '腾讯地图', pname: 'com.tencent.map', action: 'qqmap://', scheme: 'qqmap://', url: `qqmap://map/routeplan?type=drive&from=我的位置&to=${encodeURIComponent(name || '目的地')}&tocoord=${latitude},${longitude}` }); if (isIOS) { maps.push({ name: '苹果地图', pname: '', action: 'maps://', scheme: 'maps://', url: `maps://maps.apple.com/?daddr=${latitude},${longitude}&dirflg=d` }); } if (isAndroid) { maps.push({ name: '谷歌地图', pname: 'com.google.android.apps.maps', action: 'google.navigation:', scheme: 'google.navigation:', url: `google.navigation:q=${latitude},${longitude}` }); } return maps; }
async function getAvailableMaps(latitude, longitude, name = '') { const allMaps = await getAllMaps(latitude, longitude, name); const installedMaps = []; for (const map of allMaps) { const isInstalled = checkAppInstalled(map.pname, map.action); if (isInstalled) { installedMaps.push(map); } } return installedMaps; }
function openMapApp(url, mapName) { plus.runtime.openURL(url, (error) => { if (error) { uni.showToast({ title: `未安装${mapName}`, icon: 'none' }); } }); }
export async function openNavigation(latitude, longitude, name = '', options = {}) { if (!latitude || !longitude) { uni.showToast({ title: '经纬度不能为空', icon: 'none' }); return; } try { const maps = await getAvailableMaps(latitude, longitude, name); if (maps.length === 0) { uni.showModal({ title: '提示', content: '请安装地图应用(高德地图、百度地图、腾讯地图等)', showCancel: false, confirmText: '知道了' }); return; } const itemList = maps.map(map => map.name); uni.showActionSheet({ title: options.title || '选择地图应用', itemList: itemList, cancelText: options.cancelText || '取消', success: (res) => { const selectedMap = maps[res.tapIndex]; openMapApp(selectedMap.url, selectedMap.name); }, fail: (err) => { if (err.errMsg !== 'showActionSheet:cancel') { console.error('显示地图选择失败:', err); } } }); } catch (error) { console.error('打开导航失败:', error); uni.showToast({ title: '打开导航失败', icon: 'none' }); } }
|