import gcoord from "gcoord"; import { getCoord } from "@/utils/LonLatConvert.js" // gaode export const openMap = (parmas = {}) => { const { name, code, type } = parmas; uni.showLoading() getCoord({ name, code }).then(res => { const { lng, lat } = res; openApp({ lng, lat , type , name }) }).catch(err => { uni.hideLoading() uni.showToast({ title: '打开导航失败', icon: "none" }) }) } const openApp = (parmas = {}) => { uni.hideLoading() const { lng, lat , type , name } = parmas; let url; switch (type) { case 'baidu': url = `baidumap://map/marker?location=${lat},${lat}&title=${encodeURIComponent(name)}&content=${encodeURIComponent(name)}&src=webapp`; break; case 'gaode': url = `iosamap://navi?sourceApplication=webapp&backScheme=myapp://&lat=${lat}&lon=${lng}&name=${encodeURIComponent(name)}&dev=0`; break; case 'tencent': url = `qqmap://map/rgeo?location=${lat},${lat}&name=${encodeURIComponent(name)}&coord_type=1&policy=0`; break; default: console.error('不支持的地图类型'); return; } // 使用iframe打开URL Scheme, 避免直接跳转导致页面卡死 const iframe = document.createElement('iframe'); iframe.style.display = 'none'; iframe.src = url; document.body.appendChild(iframe); setTimeout(() => { document.body.removeChild(iframe); }, 1000); // 定时器检测是否唤起成功,如果失败则跳转到网页版地图 let timer = setTimeout(() => { window.location.href = `https://uri.amap.com/marker?position=${lng},${lat}&name=${encodeURIComponent(name)}&src=mypage&coordinate=gaode&callnative=0`; // 默认跳转高德网页版 }, 2000); window.addEventListener('blur', () => { clearTimeout(timer) }); }