港口定位專案開發筆記2·微信小程式端
點擊按鈕獲取在地圖上查看自己的位置
微信小程式開發中解決位置授權問題
點擊按鈕獲取在地圖上查看自己的位置
demo.wxml
<view class="title">使用微信內置地圖查看位置</view>
<button type="primary" bindtap="openLoca">查看當前位置</button>
demo.wxss
<view class="title">使用微信內置地圖查看位置</view>
<button type="primary" bindtap="openLoca">查看當前位置</button>
demo.js
openLoca(){
wx.getLocation({
type: 'gcj02', //回傳可以用于wx.openLocation的經緯度
success (res) {
const latitude = res.latitude
const longitude = res.longitude
wx.openLocation({
latitude,
longitude,
scale: 18
})
}
})
},
點擊查看當前位置即可 #### 微信小程式開發中解決位置授權問題 上篇文章中介紹了如何獲取當前位置的代碼,但是很多時候用戶不會自己打開位置授權,所以我們需要提示用戶打開位置授權功能,
wx.getLocation({
success: res => {
console.log(res);
},
fail: e => {
console.log(e);
// 判斷用戶是否拒絕了授權
wx.getSetting({
success: res => {
if (typeof(res.authSetting['scope.userLocation']) != 'undefined' && !res.authSetting['scope.userLocation']) {
// 用戶拒絕了授權
wx.showModal({
title: '提示',
content: '您拒絕了定位權限,將無法使用XX功能',
success: res => {
if (res.confirm) {
// 跳轉設定頁面
wx.openSetting({
success: res => {
if (res.authSetting['scope.userLocation']) {
// 授權成功,重新定位
wx.getLocation({
success: res => {}
});
} else {
// 沒有允許定位權限
wx.showToast({
title: '您拒絕了定位權限,將無法使用XX功能',
icon: 'none'
});
}
}
});
}
}
});
}
}
});
}
})

轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/244708.html
標籤:其他
