微信小程序如何通过当前授权位置获取用户当前所在城市,街道

如上图

从最新的微信小程序开发文档中查阅,通过微信小程序接口 wx.getLocation() 依然不能够直接获取到用户具体城市信息。本文写的主要是如何通过从微信API获取的经纬度信息转化为具体的城市街道信息。

首先如果开发者想要获取用户当前的位置,地理信息等,需要先在app.json中配置permission字段,如果用wepy开发的话,需在app.wpy做相应的配置:如下

permission: {
  "scope.userLocation": {
     "desc": "你的位置信息将用于小程序位置接口的效果展示"
  }

第二步:进入页面,调用wx.getSetting拿到用户的授权情况。

第三步:调用wx.getLocation API 获取返回的经纬度,速度等信息

第四步:因为微信没有将经纬度直接转换为地理位置,因此这里需要通过借助微信位置服务,或者百度地图开放平台的全球逆地址编码。(这里本人利用的是百度开放平台,这里ak码是需要向百度地图开放平台获取的~,请求时填入相应的ak码,经纬度等信息。

第五步:用户进入页面的时候,可在页面的onShow方法中执行wx.getLocation让用户进行相应的授权,以后每次进入该页面时,通过wx.getSetting接口,返回用户授权具体信息。

具体代码如下:

onShow() {
    this._getUserLocation();
}
  _getUserLocation () {
    var self = this
    wx.getSetting({
      success: (res) => {
        console.log('用户授权情况', res)
        //未授权
        if(res.authSetting['scope.userLocation'] !== undefined &&     
         res.authSetting['scope.userLocation'] !== true) {
          wx.showModal({
            title: '请求授权当前位置',
            content: '需要获取您的地理位置,请确认授权',
            success: function (res) {
              console.log(res)
              if(res.cancel){
                wx.showToast({
                  title: '拒绝授权',
                  icon: 'none',
                  duration: 1000
                })
              } else if (res.confirm) { //确认授权, 通过wx.openSetting发起授权请求
                wx.openSetting({
                  success: function (res) {
                    if(res.authSetting["scope.userLocation"] == true) {
                      wx.showModal({
                        title: '授权成功',
                        icon: 'success',
                        duration: 1000
                      })
                      self._getCityLocation()
                    } else {
                      wx.showModal({
                        title: '授权失败',
                        icon: 'none',
                        duration: 1000
                      })
                    }
                  }
                })
              }
            }
          })
        } else if (res.authSetting['scope.userLocation'] == undefined) {
          self._getCityLocation()
          console.log('这个为undefined')
        } else {
          console.log('授权成功')
          self._getCityLocation()
        }
      }
    })
  }
  _getCityLocation(){
    let self = this
    wx.getLocation({
      type: 'wgx84',
      success: (res) => {
        let latitude = res.latitude
        let longitude = res.longitude
        let speed = res.speed
        wx.request({
          url: 'http://api.map.baidu.com/geocoder/v2/?ak=Vh0ALNzHjjEm5RP0Ie16dlBhZbdEQip9&location=' + res.latitude + ',' + res.longitude + '&output=json',
          data: {},
          header: { 'Content-type': 'application/json' },
          success: function (ops){ 
            console.log(ops)
            self.address = ops.data.result.addressComponent.city + 
            ops.data.result.addressComponent.district
          },
          fail: function (resq) {
            wx.showModal({
              title: '信息提示',
              content: '请求失败',
              showCancel: false,
              confirmColor: '#f37938'
            });
          }
        })
      },
      fail: (res) => {
        wx.showModal({
          title: '信息提示',
          content: '请求失败',
          showCancel: false,
          comfirmColor: '#f37938'
        })
      }
    })
  }

效果如下

用户首次进入页面时会先先请求用户授权地理位置权限

用户拒绝授权后

用户拒绝授权后再次进入该页面:

用户同意授权后:

 

  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值