小程式 獲取地理位置-- wx.getLocation
阿新 • • 發佈:2018-11-12
話不多說直接上栗子
在 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); } }); },
有錯誤的地方還望大神指點一二