html頁面
<view class="map" bindtap='map'>
定位
</view>
js頁面
map() {
// 在組件實例進入頁面節點樹時執行
var _this = this
wx.getSetting({
success(res) {
// 1. scope.userLocation 為真, 代表用戶已經授權
if (res.authSetting['scope.userLocation']) {
// 1.1 使用 getlocation 獲取用戶 經緯度位置
wx.getLocation({
type: 'gcj02',
success(res) {
// 1.2 獲取用戶位置成功后,將會返回 latitude, longitude 兩個字段,代表用戶的經緯度位置
// 1.3 將獲取到的 經緯度傳值給 getAddress 解析出 具體的地址
_this.getAddress(res.latitude, res.longitude)
}
})
} else {
// 2. 用戶未授權的情況下, 打開授權界面, 引導用戶授權.
wx.openSetting({
success(res) {
// 2.1 如果二次授權允許了 userLocation 權限, 就再次執行獲取位置的接口
if (res.authSetting["scope.userLocation"]) {
wx.getLocation({
success(res) {
// 2.2 獲取用戶位置成功后,將會返回 latitude, longitude 兩個字段,代表用戶的經緯度位置
// 2.3 將獲取到的 經緯度傳值給 getAddress 解析出 具體的地址
_this.getAddress(res.latitude, res.longitude)
}
})
}
}
})
}
}
})
}