微信小程序通过地图选择位置

641 阅读1分钟
// 打开地图选择位置
  chooseLocation() {
    let _this = this
    // 首先判断一下是否有权限
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userLocation']) {
          // 没有权限,就调起授权页面
          wx.authorize({
            scope: 'scope.userLocation',
            success(res2) {
              // 有权限,该方法就拉起了地图
              wx.chooseLocation({
                latitude: 0,
                success(res) {
                  _this.setData({
                    addPlace: res2.name
                  })
                }
              })
            },
            fail() {
              // 如果用户点击了拒绝,那么再次调用该方法会直接进入fail
              Dialog.alert({
                title: '提示',
                message: '您已拒绝位置授权,若你想使用这项功能,请打开位置授权',
              }).then(() => {
                // 因此调用wx的权限页面,让用户自己打开权限
                wx.openSetting({
                  withSubscriptions: true,
                })
              });

            }
          })
        } else {
          // 有权限,该方法就拉起了地图
          wx.chooseLocation({
            latitude: 0,
            success(res) {
              _this.setData({
                addPlace: res.name
              })
            }
          })
        }
      }
    })
  }