話不多說直接上栗子
//首先聲明變量
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); } }); },
有錯誤的地方還望大神指點一二
