微信小程序定位wx.getLocation

小程序定位功能,虽然功能也完善,但是首次接触还是挺疑惑,看了下逻辑还是明白些了,总结如下:

一、微信开发者文档:

wx.getLocation(Object object)链接地址:https://developers.weixin.qq.com/miniprogram/dev/api/location/wx.getLocation.html
二、代码贴上:

selfLocation() {
  const self = this
  uni.showLoading({
    title: '定位中',
    mask: true,
  });
  uni.getLocation({
    type: 'gcj02',
    success: (res) => {
      let latitude, longitude;
      latitude = res.latitude.toString();
      longitude = res.longitude.toString();
      this.latitude = res.latitude
      this.longitude = res.longitude
      getGeocoder({lat: latitude,long: longitude}).then(res => { // 获取详细信息的接口
         const data = res.data;
             self.userAddress.userAddressdetail = ''
         var params = {
           text: data.address
         }
         parseAddress(params).then(res => { // 粘贴详细信息的接口
            console.log(res)
            if(res.status==200 && res.message=="success"){
             this.$forceUpdate();   // 定位后,界面没有反应,因此加上强制刷新                              
                         this.userAddress.userAddressdetail = res.data.town + res.data.detail;
                         this.$set( this.userAddress, 'selectAddress', parseInt(res.data.county_info.city_id));
             this.addressInfo[0] = res.data.province_info ? res.data.province_info : {};
             this.addressInfo[1] = res.data.city_info ? res.data.city_info : {};
             this.addressInfo[2] = res.data.county_info ? res.data.county_info : {};
          }
      }).catch(res => {
         console.log("没有地址信息")
     })
     uni.hideLoading();
 })
},
fail: (res) => {
  console.log(res)
  uni.hideLoading();
    uni.showToast({
        title: res.errMsg,
        icon: 'none',
        duration: 1000
    });
    }
  });
},

二、注意事项:点击定位按钮,界面没有反应,但是点击返回,确定位成功,因此加上this.$forceUpdate()即可;

三、报错:getLocation:fail the api need to be declared in the requiredPrivateInfos

经查发现:检查app.json的配置,发现无误

"permission" : {
     "scope.userLocation" : {
           "desc" : "获取您的位置"
     }
},

编辑器问题消失,手机体验版依然有问题,问题在继续解决中。。。。
查找资料发现,手机体验版不可以开启定位,只能正式版。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容