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

话不多说直接上栗子

//首先声明变量
data:{ showLocationAuth:fasle }
//这是第一种逻辑实现方式 点击按钮
//当第一次点击授权按钮,用户取消授权之后,就会显示 授权当前定位按钮 点击之后就会触发系统授权系统授权成功之后在次点击 getShopLocationPoint 方法 就能拿到经纬度

<button   wx:if="{{!showLocationAuth}}"   class="get_location_btn"   bindtap='getShopLocationPoint'   >   点我  </button>
<button wx:if="{{showLocationAuth}}" class="get_location_btn" open-type="openSetting" > 授权当前定位 </button>

/*************我是分割线************/

//这是第二种逻辑 实现方式 
onShow(){
  this.getShopLocationPoint(); 在这里调用是因为进入页面需要获取,如果用户没有授权,就去点击wxml的按钮唤醒系统授权,开启系统授权之后关闭系统页面,ohShow 之后就能拿到经纬度 
}
<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);
      }
    });

  },

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