index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. // 交换 name 和 address 的显示
  63. // 微信地图中 name 显示在上面(大字),address 显示在下面(小字)
  64. // 需求:上面显示地址,下面显示门店名
  65. wx.openLocation({
  66. latitude: parseFloat(latitude),
  67. longitude: parseFloat(longitude),
  68. name: address || name, // 上面显示地址
  69. address: name || address, // 下面显示门店名
  70. scale: 18,
  71. success: () => {
  72. console.log('✅ 地图打开成功');
  73. this.setData({ loading: false });
  74. },
  75. fail: (err) => {
  76. console.error('❌ 打开地图失败:', err);
  77. this.setData({
  78. loading: false,
  79. errorMsg: '打开地图失败'
  80. });
  81. wx.showModal({
  82. title: '提示',
  83. content: '打开地图失败,请检查是否授权位置权限',
  84. showCancel: true,
  85. cancelText: '返回',
  86. confirmText: '重试',
  87. success: (res) => {
  88. if (res.confirm) {
  89. // 重试
  90. this.openLocation(latitude, longitude, name, address);
  91. } else {
  92. // 返回
  93. wx.navigateBack({
  94. fail: () => {
  95. wx.switchTab({
  96. url: '/pages/index/index',
  97. fail: () => {
  98. wx.reLaunch({
  99. url: '/pages/index/index'
  100. });
  101. }
  102. });
  103. }
  104. });
  105. }
  106. }
  107. });
  108. },
  109. complete: () => {
  110. // 打开地图后延迟返回,给用户时间查看
  111. setTimeout(() => {
  112. wx.navigateBack({
  113. fail: () => {
  114. console.log('返回失败,可能是首页');
  115. }
  116. });
  117. }, 500);
  118. }
  119. });
  120. },
  121. onUnload: function () {
  122. console.log('地图导航页面卸载');
  123. }
  124. });