小程序 获取地理位置-- wx.getLocation

话不多说直接上栗子

在 onShow里面调用 

onShow(){
  this.getShopLocationPoint(); 在这里调用是因为需要获取 地址的时候可能用户不授权,当取消授权的时候没办法拿到经纬度,此时我们要加判断了    
}

wxml 如下:

<button 
  wx:if="{{showLocationAuth}}" 
  class="get_location_btn"
  open-type="openSetting"
>
  授权当前定位
</button>
//这是需要调用系统授权,只要授权之后 就会触发 onShow 里面的
this.getShopLocationPoint() 就会获取到用户的经纬度信息
 
  getShopLocationPoint() {
    this.setData({
      showLocationAuth: false
    });
   wx.getLocation({
      type: "wgs84", // 默认wgs84
      success: res => {
        this.setData({
          showLocationAuth: false
        });
        console.log(res)
      },
      fail: res => {
        this.setData({
          showLocationAuth: true
        });
        console.log(res);
      }
    });

  },

有错误的地方还望大神指点一二

猜你喜欢

转载自www.cnblogs.com/BeautifulBoy/p/9790309.html
今日推荐