微信小程序獲取地理位置及經(jīng)緯度授權(quán)代碼實(shí)例
這篇文章主要介紹了微信小程序獲取地理位置及經(jīng)緯度授權(quán)代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
微信小程序獲取地理位置授權(quán),首先需要在app.json中添加配置:
"permission": {
"scope.userLocation": {
"desc": "請確認(rèn)授權(quán)"
}
}
獲取經(jīng)緯度:如果手機(jī)未開啟位置信息,那么授權(quán)成功后在wx.getLocation()方法中也會一直失敗,所以需要在fail方法中提示用戶開啟手機(jī)位置信息
getUserLocation: function () {
let vm = this
wx.getSetting({
success: (res) => {
// res.authSetting['scope.userLocation'] == undefined 表示 初始化進(jìn)入該頁面
// res.authSetting['scope.userLocation'] == false 表示 非初始化進(jìn)入該頁面,且未授權(quán)
// res.authSetting['scope.userLocation'] == true 表示 地理位置授權(quán)
// 拒絕授權(quán)后再次進(jìn)入重新授權(quán)
if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
// console.log('authSetting:status:拒絕授權(quán)后再次進(jìn)入重新授權(quán)', res.authSetting['scope.userLocation'])
wx.showModal({
title: '',
content: '【泰福利Lite】需要獲取你的地理位置,請確認(rèn)授權(quán)',
success: function (res) {
if (res.cancel) {
wx.showToast({
title: '拒絕授權(quán)',
icon: 'none'
})
setTimeout(() => {
wx.navigateBack()
}, 1500)
} else if (res.confirm) {
wx.openSetting({
success: function (dataAu) {
// console.log('dataAu:success', dataAu)
if (dataAu.authSetting["scope.userLocation"] == true) {
//再次授權(quán),調(diào)用wx.getLocation的API
vm.getLocation(dataAu)
} else {
wx.showToast({
title: '授權(quán)失敗',
icon: 'none'
})
setTimeout(() => {
wx.navigateBack()
}, 1500)
}
}
})
}
}
})
}
// 初始化進(jìn)入,未授權(quán)
else if (res.authSetting['scope.userLocation'] == undefined) {
// console.log('authSetting:status:初始化進(jìn)入,未授權(quán)', res.authSetting['scope.userLocation'])
//調(diào)用wx.getLocation的API
vm.getLocation(res)
}
// 已授權(quán)
else if (res.authSetting['scope.userLocation']) {
// console.log('authSetting:status:已授權(quán)', res.authSetting['scope.userLocation'])
//調(diào)用wx.getLocation的API
vm.getLocation(res)
}
}
})
},
// 微信獲得經(jīng)緯度
getLocation: function (userLocation) {
let vm = this
wx.getLocation({
type: "wgs84",
success: function (res) {
// console.log('getLocation:success', res)
var latitude = res.latitude
var longitude = res.longitude
vm.getDaiShu(latitude, longitude)
},
fail: function (res) {
// console.log('getLocation:fail', res)
if (res.errMsg === 'getLocation:fail:auth denied') {
wx.showToast({
title: '拒絕授權(quán)',
icon: 'none'
})
setTimeout(() => {
wx.navigateBack()
}, 1500)
return
}
if (!userLocation || !userLocation.authSetting['scope.userLocation']) {
vm.getUserLocation()
} else if (userLocation.authSetting['scope.userLocation']) {
wx.showModal({
title: '',
content: '請?jiān)谙到y(tǒng)設(shè)置中打開定位服務(wù)',
showCancel: false,
success: result => {
if (result.confirm) {
wx.navigateBack()
}
}
})
} else {
wx.showToast({
title: '授權(quán)失敗',
icon: 'none'
})
setTimeout(() => {
wx.navigateBack()
}, 1500)
}
}
})
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript基于對象方法實(shí)現(xiàn)數(shù)組去重及排序操作示例
這篇文章主要介紹了JavaScript基于對象方法實(shí)現(xiàn)數(shù)組去重及排序操作,涉及javascript基于對象方法的數(shù)組遍歷、比較、去重、排序等相關(guān)操作技巧,需要的朋友可以參考下2018-07-07
js+css3實(shí)現(xiàn)旋轉(zhuǎn)效果
本文主要介紹了js+css3實(shí)現(xiàn)旋轉(zhuǎn)效果的方法。具有一定的參考價(jià)值,下面跟著小編一起來看下吧2017-01-01
如何快速高效創(chuàng)建JavaScript 一維數(shù)組方法詳解
這篇文章主要為大家介紹了如何快速高效創(chuàng)建JavaScript 一維數(shù)組方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
使用Math.max,Math.min獲取數(shù)組中的最值實(shí)例
下面小編就為大家?guī)硪黄褂肕ath.max,Math.min獲取數(shù)組中的最值實(shí)例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04
JavaScript使用小插件實(shí)現(xiàn)倒計(jì)時的方法講解
今天小編就為大家分享一篇關(guān)于JavaScript使用小插件實(shí)現(xiàn)倒計(jì)時的方法講解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03

