openApp.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import gcoord from "gcoord";
  2. import { getCoord } from "@/utils/LonLatConvert.js"
  3. // gaode
  4. export const openMap = (parmas = {}) => {
  5. const { name, code, type } = parmas;
  6. uni.showLoading()
  7. getCoord({ name, code }).then(res => {
  8. const { lng, lat } = res;
  9. openApp({ lng, lat , type , name })
  10. }).catch(err => {
  11. uni.hideLoading()
  12. uni.showToast({
  13. title: '打开导航失败',
  14. icon: "none"
  15. })
  16. })
  17. }
  18. const openApp = (parmas = {}) => {
  19. uni.hideLoading()
  20. const { lng, lat , type , name } = parmas;
  21. let url;
  22. switch (type) {
  23. case 'baidu':
  24. url =
  25. `baidumap://map/marker?location=${lat},${lat}&title=${encodeURIComponent(name)}&content=${encodeURIComponent(name)}&src=webapp`;
  26. break;
  27. case 'gaode':
  28. url =
  29. `iosamap://navi?sourceApplication=webapp&backScheme=myapp://&lat=${lat}&lon=${lng}&name=${encodeURIComponent(name)}&dev=0`;
  30. break;
  31. case 'tencent':
  32. url =
  33. `qqmap://map/rgeo?location=${lat},${lat}&name=${encodeURIComponent(name)}&coord_type=1&policy=0`;
  34. break;
  35. default:
  36. console.error('不支持的地图类型');
  37. return;
  38. }
  39. // 使用iframe打开URL Scheme, 避免直接跳转导致页面卡死
  40. const iframe = document.createElement('iframe');
  41. iframe.style.display = 'none';
  42. iframe.src = url;
  43. document.body.appendChild(iframe);
  44. setTimeout(() => {
  45. document.body.removeChild(iframe);
  46. }, 1000);
  47. // 定时器检测是否唤起成功,如果失败则跳转到网页版地图
  48. let timer = setTimeout(() => {
  49. window.location.href =
  50. `https://uri.amap.com/marker?position=${lng},${lat}&name=${encodeURIComponent(name)}&src=mypage&coordinate=gaode&callnative=0`; // 默认跳转高德网页版
  51. }, 2000);
  52. window.addEventListener('blur', () => {
  53. clearTimeout(timer)
  54. });
  55. }