| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- /**
- * 地图导航页面
- * 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) {
- wx.openLocation({
- latitude: parseFloat(latitude),
- longitude: parseFloat(longitude),
- name: name,
- address: 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('地图导航页面卸载');
- }
- });
|