|
|
@@ -5,43 +5,129 @@
|
|
|
* 示例: wx.miniProgram.navigateTo({url: '/common-page/pages/map-open/index?latitude=39.908&longitude=116.397&name=天安门&address=北京市东城区'})
|
|
|
*/
|
|
|
Page({
|
|
|
- data: {},
|
|
|
+ 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'
|
|
|
+ icon: 'none',
|
|
|
+ duration: 2000
|
|
|
});
|
|
|
+
|
|
|
setTimeout(() => {
|
|
|
- wx.navigateBack();
|
|
|
- }, 1500);
|
|
|
+ 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) {
|
|
|
wx.openLocation({
|
|
|
latitude: parseFloat(latitude),
|
|
|
longitude: parseFloat(longitude),
|
|
|
- name: decodeURIComponent(name || ''),
|
|
|
- address: decodeURIComponent(address || ''),
|
|
|
+ name: name,
|
|
|
+ address: address,
|
|
|
scale: 18,
|
|
|
success: () => {
|
|
|
console.log('✅ 地图打开成功');
|
|
|
+ this.setData({ loading: false });
|
|
|
},
|
|
|
fail: (err) => {
|
|
|
console.error('❌ 打开地图失败:', err);
|
|
|
- wx.showToast({
|
|
|
- title: '打开地图失败',
|
|
|
- icon: 'none'
|
|
|
+
|
|
|
+ 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: () => {
|
|
|
- // 打开地图后返回上一页
|
|
|
- wx.navigateBack();
|
|
|
+ // 打开地图后延迟返回,给用户时间查看
|
|
|
+ setTimeout(() => {
|
|
|
+ wx.navigateBack({
|
|
|
+ fail: () => {
|
|
|
+ console.log('返回失败,可能是首页');
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }, 500);
|
|
|
}
|
|
|
});
|
|
|
+ },
|
|
|
+
|
|
|
+ onUnload: function () {
|
|
|
+ console.log('地图导航页面卸载');
|
|
|
}
|
|
|
});
|