微信小程序獲取地理位置及經(jīng)緯度授權(quán)代碼實(shí)例
這篇文章主要介紹了微信小程序獲取地理位置及經(jīng)緯度授權(quán)代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
微信小程序獲取地理位置授權(quán),首先需要在app.json中添加配置:
"permission": {
"scope.userLocation": {
"desc": "請(qǐng)確認(rèn)授權(quán)"
}
}
獲取經(jīng)緯度:如果手機(jī)未開啟位置信息,那么授權(quán)成功后在wx.getLocation()方法中也會(huì)一直失敗,所以需要在fail方法中提示用戶開啟手機(jī)位置信息
getUserLocation: function () {
let vm = this
wx.getSetting({
success: (res) => {
// res.authSetting['scope.userLocation'] == undefined 表示 初始化進(jìn)入該頁(yè)面
// res.authSetting['scope.userLocation'] == false 表示 非初始化進(jìn)入該頁(yè)面,且未授權(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】需要獲取你的地理位置,請(qǐng)確認(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: '請(qǐng)?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)
}
}
})
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
js實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)重定向的幾種方式
這篇文章主要介紹js實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)重定向的幾種方式,需要的朋友可以參考下2014-05-05
TypeScript裝飾器與反射元數(shù)據(jù)實(shí)例詳解
TypeScript的裝飾器為我們提供了一種強(qiáng)大的工具,可以在運(yùn)行時(shí)改變類的行為,通過(guò)理解裝飾器的工作原理,我們可以創(chuàng)造更加強(qiáng)大、靈活且易于維護(hù)的應(yīng)用,這篇文章主要介紹了TypeScript裝飾器與反射元數(shù)據(jù),需要的朋友可以參考下2023-09-09
bootstrap-table獲取表格數(shù)據(jù)的多種方式
這篇文章主要介紹了bootstrap-table獲取表格數(shù)據(jù)的多種方式,bootstrap-table獲取值得兩種方式,一種是通過(guò)data獲取,一種是通過(guò)url獲取,需要的朋友可以參考下2023-10-10
video.js 一個(gè)頁(yè)面同時(shí)播放多個(gè)視頻的實(shí)例代碼
這篇文章主要介紹了video.js 一個(gè)頁(yè)面同時(shí)播放多個(gè)視頻的實(shí)例代碼 ,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-11-11
JavaScript中的16進(jìn)制字符(改進(jìn))
后來(lái)經(jīng)過(guò)自己的測(cè)試,發(fā)現(xiàn)將字符轉(zhuǎn)換為十六進(jìn)制的方法不完善。2011-11-11
用JavaScript實(shí)現(xiàn)PHP的urlencode與urldecode函數(shù)
這篇文章主要介紹了用JavaScript實(shí)現(xiàn)PHP的urlencode與urldecode函數(shù),很多情況下我們用了出來(lái)php urlencode出來(lái)的網(wǎng)址,需要的朋友可以參考下2015-08-08
當(dāng)某個(gè)文本框成為焦點(diǎn)時(shí)即清除文本框內(nèi)容
這篇文章主要介紹了當(dāng)某個(gè)文本框成為焦點(diǎn)時(shí)如何清除文本框內(nèi)容,需要的朋友可以參考下2014-04-04
JavaScript實(shí)現(xiàn)的圖片3D展示空間(3DRoom)
一般的平面效果,通過(guò)改變水平和垂直坐標(biāo)就能實(shí)現(xiàn),再加上深度,就能在視覺(jué)上的產(chǎn)生3D(三維)的效果。2010-10-10

