微信小程序关于位置location(二)

1,388 阅读1分钟

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第6天,点击查看活动详情

wx.chooseAddress

获取用户收货地址。调起微信的用户收货地址界面,可以在界面新增编辑收货地址,并选择地址,确定之后将返回用户选择的地址。

image.png

代码如下

  chooseAddress() {
    wx.chooseAddress({
      success: (res) => {
        console.log(res)
      },
      fail: function (err) {
        console.log(err)
      }
    })
  }

打印信息

image.png

需要在 app.json 中 requiredPrivateInfos 配置项中声明

image.png

wx.choosePoi

打开 POI 列表选择位置,它支持模糊定位(精确到市)和精确定位两种

wx.getSetting

获取用户的当前设置。返回值中只会出现小程序已经向用户请求过的权限。

 wx.getSetting({
  withSubscriptions: true,
  success: (res) => {
    console.log(res)
  },
  fail: function (err) {
    console.log(err)
  }
})

image.png

wx.openSetting

打开设置界面,引导用户开启授权。 该接口会返回用户设置的结果。设置界面只会出现小程序已经向用户请求过的权限。 可选传参withSubscriptions,是否同时获取用户订阅消息的订阅状态。

wx.openSetting({
  success (res) {
    // 调用成功,返回用户授权结果
    console.log(res.authSetting)
    // res.authSetting = {
    //   "scope.userInfo": true,
    //   "scope.userLocation": true
    // }
  }
})

wx.authorize

该接口会提前向用户发起授权请求。调用后会立刻弹窗询问用户是否同意授权小程序使用某项功能或获取用户的某些数据,但不会实际调用对应接口。如果用户之前已经同意授权,则不会出现弹窗,直接返回成功

// 先通过 wx.getSetting 查询用户是否授权了 "scope.userLocation" 
// 没有的话用wx.authorize 调用弹框,询问用户是否要同意授权
wx.getSetting({
  success(res) {
    if (!res.authSetting['scope.userLocation']) {
      wx.authorize({
        scope: 'scope.userLocation',
        success () {
          // 处理逻辑
        }
      })
    }
  }
})