/** * 地图导航页面 * H5 通过 wx.miniProgram.navigateTo 跳转到此页面 * 参数: latitude, longitude, name, address * 示例: wx.miniProgram.navigateTo({url: '/common-page/pages/map-open/index?latitude=39.908&longitude=116.397&name=天安门&address=北京市东城区'}) */ Page({ data: { loading: true, errorMsg: '' }, onLoad: function (options) { console.log('==========================================='); console.log('======= 地图导航页面 ======='); console.log('接收参数:', options); console.log('==========================================='); const { latitude, longitude, name, address } = options; // 参数验证 if (!latitude || !longitude) { console.error('❌ 缺少位置信息'); this.setData({ loading: false, errorMsg: '缺少位置信息' }); wx.showToast({ title: '缺少位置信息', icon: 'none', duration: 2000 }); setTimeout(() => { wx.navigateBack({ fail: () => { // 如果返回失败,跳转到首页 wx.switchTab({ url: '/pages/index/index', fail: () => { wx.reLaunch({ url: '/pages/index/index' }); } }); } }); }, 2000); return; } // 解码参数 const decodedName = name ? decodeURIComponent(name) : '目的地'; const decodedAddress = address ? decodeURIComponent(address) : ''; console.log('📍 位置信息:'); console.log(' - 纬度:', latitude); console.log(' - 经度:', longitude); console.log(' - 名称:', decodedName); console.log(' - 地址:', decodedAddress); // 直接打开地图导航 this.openLocation(latitude, longitude, decodedName, decodedAddress); }, /** * 打开地图 */ openLocation(latitude, longitude, name, address) { // 交换 name 和 address 的显示 // 微信地图中 name 显示在上面(大字),address 显示在下面(小字) // 需求:上面显示地址,下面显示门店名 wx.openLocation({ latitude: parseFloat(latitude), longitude: parseFloat(longitude), name: address || name, // 上面显示地址 address: name || address, // 下面显示门店名 scale: 18, success: () => { console.log('✅ 地图打开成功'); this.setData({ loading: false }); }, fail: (err) => { console.error('❌ 打开地图失败:', err); this.setData({ loading: false, errorMsg: '打开地图失败' }); wx.showModal({ title: '提示', content: '打开地图失败,请检查是否授权位置权限', showCancel: true, cancelText: '返回', confirmText: '重试', success: (res) => { if (res.confirm) { // 重试 this.openLocation(latitude, longitude, name, address); } else { // 返回 wx.navigateBack({ fail: () => { wx.switchTab({ url: '/pages/index/index', fail: () => { wx.reLaunch({ url: '/pages/index/index' }); } }); } }); } } }); }, complete: () => { // 打开地图后延迟返回,给用户时间查看 setTimeout(() => { wx.navigateBack({ fail: () => { console.log('返回失败,可能是首页'); } }); }, 500); } }); }, onUnload: function () { console.log('地图导航页面卸载'); } });