index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. * 地图导航页面
  3. * H5 通过 wx.miniProgram.navigateTo 跳转到此页面
  4. * 参数: latitude, longitude, name, address
  5. * 示例: wx.miniProgram.navigateTo({url: '/common-page/pages/map-open/index?latitude=39.908&longitude=116.397&name=天安门&address=北京市东城区'})
  6. */
  7. Page({
  8. data: {
  9. loading: true,
  10. errorMsg: ''
  11. },
  12. onLoad: function (options) {
  13. console.log('===========================================');
  14. console.log('======= 地图导航页面 =======');
  15. console.log('接收参数:', options);
  16. console.log('===========================================');
  17. const { latitude, longitude, name, address } = options;
  18. // 参数验证
  19. if (!latitude || !longitude) {
  20. console.error('❌ 缺少位置信息');
  21. this.setData({
  22. loading: false,
  23. errorMsg: '缺少位置信息'
  24. });
  25. wx.showToast({
  26. title: '缺少位置信息',
  27. icon: 'none',
  28. duration: 2000
  29. });
  30. setTimeout(() => {
  31. wx.navigateBack({
  32. fail: () => {
  33. // 如果返回失败,跳转到首页
  34. wx.switchTab({
  35. url: '/pages/index/index',
  36. fail: () => {
  37. wx.reLaunch({
  38. url: '/pages/index/index'
  39. });
  40. }
  41. });
  42. }
  43. });
  44. }, 2000);
  45. return;
  46. }
  47. // 解码参数
  48. const decodedName = name ? decodeURIComponent(name) : '目的地';
  49. const decodedAddress = address ? decodeURIComponent(address) : '';
  50. console.log('📍 位置信息:');
  51. console.log(' - 纬度:', latitude);
  52. console.log(' - 经度:', longitude);
  53. console.log(' - 名称:', decodedName);
  54. console.log(' - 地址:', decodedAddress);
  55. // 直接打开地图导航
  56. this.openLocation(latitude, longitude, decodedName, decodedAddress);
  57. },
  58. /**
  59. * 打开地图
  60. */
  61. openLocation(latitude, longitude, name, address) {
  62. wx.openLocation({
  63. latitude: parseFloat(latitude),
  64. longitude: parseFloat(longitude),
  65. name: name,
  66. address: address,
  67. scale: 18,
  68. success: () => {
  69. console.log('✅ 地图打开成功');
  70. this.setData({ loading: false });
  71. },
  72. fail: (err) => {
  73. console.error('❌ 打开地图失败:', err);
  74. this.setData({
  75. loading: false,
  76. errorMsg: '打开地图失败'
  77. });
  78. wx.showModal({
  79. title: '提示',
  80. content: '打开地图失败,请检查是否授权位置权限',
  81. showCancel: true,
  82. cancelText: '返回',
  83. confirmText: '重试',
  84. success: (res) => {
  85. if (res.confirm) {
  86. // 重试
  87. this.openLocation(latitude, longitude, name, address);
  88. } else {
  89. // 返回
  90. wx.navigateBack({
  91. fail: () => {
  92. wx.switchTab({
  93. url: '/pages/index/index',
  94. fail: () => {
  95. wx.reLaunch({
  96. url: '/pages/index/index'
  97. });
  98. }
  99. });
  100. }
  101. });
  102. }
  103. }
  104. });
  105. },
  106. complete: () => {
  107. // 打开地图后延迟返回,给用户时间查看
  108. setTimeout(() => {
  109. wx.navigateBack({
  110. fail: () => {
  111. console.log('返回失败,可能是首页');
  112. }
  113. });
  114. }, 500);
  115. }
  116. });
  117. },
  118. onUnload: function () {
  119. console.log('地图导航页面卸载');
  120. }
  121. });