小程序-打开地理位置,导航

默认在wxml上面tap绑定事件,点击打开地理位置。

  validationPosition() {
    wx.getSetting({
      success: (res) => {
        if (res.authSetting['scope.userLocation']) {
          // 定位权限已开启
          wx.getLocation({
            type: 'gcj02',
            success: function (res) {
              console.log("当前坐标信息:", res);
            }
          });
        } else if (res.authSetting['scope.userLocation'] === false) {
          // 定位权限被拒绝,引导用户前往设置页面
          wx.showModal({
            title: '您未开启地理位置授权',
            content: '请在系统设置中打开位置授权,以便我们为您提供更好的服务',
            success: (res) => {
              if (res.confirm) {
                wx.openSetting();
              }
            }
          });
        } else {
          // 首次请求定位权限
          wx.authorize({
            scope: 'scope.userLocation',
            success: () => {
              wx.getLocation({
                type: 'gcj02',
                success: function (res) {
                  console.log("当前坐标信息:", res);
                }
              });
            },
            fail: () => {
              wx.showModal({
                title: '您未开启地理位置授权',
                content: '请在系统设置中打开位置授权,以便我们为您提供更好的服务',
                success: (res) => {
                  if (res.confirm) {
                    wx.openSetting();
                  }
                }
              });
            }
          });
        }
      }
    });
  },

 

通过经纬度和地址打开导航地图:

  handleGoThere: function (e) {
    const name = e.currentTarget.dataset.name;
    wx.openLocation({
      latitude: parseFloat(this.data.latitude),
      longitude: parseFloat(this.data.longitude),
      name: name,
      scale: 14
    })
  },

 通过地址信息获取经纬度 (https://www.mapchaxun.cn/Regeo/)。

如果添加经纬度,无法在手机上定位到指定位置,在小程序后台-设置 -完善内容 -添加 [位置信息] 勾选,填写内容:获取附近位置。

微信小程序插件 | 腾讯位置服务 https://lbs.qq.com/miniProgram/plugin/pluginGuide/locationPicker

 

解决使用腾讯地图没超过额度却一直报“此key每日调用量已达到上限”,在腾讯位置服务配置额度,通过接口调用查询地址额度。

如果打开小程序,右上角没有显示-位置信息,需要 在app.json文件添加如下配置:

  "requiredPrivateInfos":[ 
    "getLocation" 
  ],

 

posted @ 2025-07-14 15:33  微宇宙  阅读(37)  评论(0)    收藏  举报