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

默认在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/)。

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

微信小程序插件 | 腾讯位置服务

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