openApp.js 2.2 KB

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