uniapp中獲取位置信息處理步驟
在微信小程序中,獲取定位,是需要用戶授權(quán)的,那么當(dāng)用戶拒絕授權(quán)后,需要重新獲取定位時(shí),是不會(huì)再調(diào)起授權(quán)界面,這時(shí)需要用戶主動(dòng)打開設(shè)置界面,才可以重新開啟授權(quán)權(quán)限;
那么,在uniapp中獲取位置信息處理,要兼容用戶同意授權(quán)、拒絕授權(quán)情況下,最終能成功獲取到位置信息的,做以下處理:
處理邏輯
一、獲取定位時(shí),用戶同意授權(quán)獲取定位,得到位置信息;
第1步:獲取用戶當(dāng)前的授權(quán)狀態(tài) =>
第2步:判斷是同意授權(quán)位置時(shí) =>
第3步:獲取位置
二、獲取定位時(shí),用戶拒絕授權(quán)獲取定位的:
第1步:獲取用戶當(dāng)前的授權(quán)狀態(tài) =>
第2步:判斷是未同意授權(quán)位置時(shí),引導(dǎo)用戶打開設(shè)置界面,重新選擇授權(quán)功能 =>
第3步:用戶選擇允許授權(quán)后
第4步:重新獲取位置,得到位置信息
第3步:用戶選擇不允許授權(quán)后
第4步:可至第1步,繼續(xù)重新獲取位置
引用文件可多頁面復(fù)用的處理邏輯代碼
引用文件:
import { doGetLocation } from '@/utils/getLocation.js';需要獲取位置代碼處執(zhí)行:
doGetLocation((data)=>{
console.log(data);
})getLocation.js:
// import { doGetLocation } from '@/utils/getLocation.js';
let isOpenSetting;
/**
* 獲取定位,兼容用戶拒絕授權(quán)及相關(guān)處理(獲取用戶當(dāng)前的授權(quán)狀態(tài) => 未同意授權(quán)位置時(shí),引導(dǎo)用戶打開設(shè)置界面,重新選擇授權(quán)功能 => 允許后重新獲取位置)
*/
export function doGetLocation(callback){
isOpenSetting = false; // 是否打開設(shè)置界面
// 獲取用戶當(dāng)前的授權(quán)狀態(tài)
uni.getSetting({
success: (settingRes) => {
console.log(settingRes)
console.log(isOpenSetting)
// 判斷用戶未同意授權(quán)位置時(shí),提示并引導(dǎo)用戶去打開設(shè)置界面,用戶可重新選擇授權(quán)功能
if (!isOpenSetting && typeof(settingRes.authSetting['scope.userLocation']) != 'undefined' && !settingRes.authSetting['scope.userLocation']) {
uni.showModal({
title: '需要授權(quán)獲取您的位置信息',
content: '你的位置信息將用于為您提供更合適您的服務(wù)',
success: (data) => {
if (data.confirm) {
isOpenSetting = true;
// 打開設(shè)置界面
uni.openSetting({
success: (response) => {
if(response.authSetting['scope.userLocation']){
console.log('重新授權(quán)獲取位置信息-同意');
// 重新獲取定位
getLocation((data)=>{
callback({
isOpenSetting:isOpenSetting,
...data
})
});
}else{
console.log('重新授權(quán)獲取位置信息-未同意');
callback({
isOpenSetting:isOpenSetting,
latitude : '',
longitude : '',
})
}
},
fail:()=>{
console.log('openSetting接口調(diào)用失敗的回調(diào)函數(shù)');
}
})
} else if (data.cancel) {
console.log('showModal接口:用戶點(diǎn)擊取消未打開設(shè)置界面');
callback({
isOpenSetting:isOpenSetting,
latitude : '',
longitude : '',
})
}
},
fail: function(){
console.log('showModal接口:調(diào)用失敗的回調(diào)函數(shù)');
}
});
}else{
// 重新獲取定位
getLocation((data)=>{
callback({
isOpenSetting:isOpenSetting,
...data
})
});
}
}
})
}
/**
* 獲取位置
*/
export function getLocation(callback){
uni.getLocation({
//type: 'wgs84',
type: 'gcj02',
success: (res)=>{
console.log(res);
callback({
latitude : res.latitude,
longitude : res.longitude,
})
},
fail: (res)=>{
console.log('用戶拒絕授權(quán)獲取位置信息,使用默認(rèn)經(jīng)緯度0 0');
callback({
latitude : '',
longitude : '',
})
},complete: (res)=>{
// console.log(res);
// 根據(jù)位置數(shù)據(jù)更新頁面數(shù)據(jù)
}
});
}直接在頁面中處理邏輯代碼
需要獲取位置代碼處執(zhí)行:
this.doGetLocation();
methods中處理方法:
methods: {
// ......
// 獲取定位,兼容用戶拒絕授權(quán)及相關(guān)處理(獲取用戶當(dāng)前的授權(quán)狀態(tài) => 未同意授權(quán)位置時(shí),引導(dǎo)用戶打開設(shè)置界面,重新選擇授權(quán)功能 => 允許后重新獲取位置)
doGetLocation(){
this.isOpenSetting = false; // 是否打開設(shè)置界面
// 獲取用戶當(dāng)前的授權(quán)狀態(tài)
uni.getSetting({
success: (settingRes) => {
console.log(settingRes)
console.log(this.isOpenSetting)
// 判斷用戶未同意授權(quán)位置時(shí),提示并引導(dǎo)用戶去打開設(shè)置界面,用戶可重新選擇授權(quán)功能
if (!this.isOpenSetting && typeof(settingRes.authSetting['scope.userLocation']) != 'undefined' && !settingRes.authSetting['scope.userLocation']) {
uni.showModal({
title: '需要授權(quán)獲取您的位置信息',
content: '你的位置信息將用于為您提供更合適您的服務(wù)',
success: (data) => {
if (data.confirm) {
this.isOpenSetting = true;
// 打開設(shè)置界面
uni.openSetting({
success: (response) => {
if(response.authSetting['scope.userLocation']){
console.log('重新授權(quán)獲取位置信息-同意');
// 重新獲取定位
this.getLocation();
}else{
console.log('重新授權(quán)獲取位置信息-未同意');
this.doGetLocationAfter({
latitude : '',
longitude : '',
isOpenSetting : this.isOpenSetting,
})
}
},
fail:()=>{
console.log('openSetting接口調(diào)用失敗的回調(diào)函數(shù)');
}
})
} else if (data.cancel) {
console.log('showModal接口:用戶點(diǎn)擊取消未打開設(shè)置界面');
this.doGetLocationAfter({
latitude : '',
longitude : '',
isOpenSetting : this.isOpenSetting,
})
}
},
fail: function(){
console.log('showModal接口:調(diào)用失敗的回調(diào)函數(shù)');
}
});
}else{
// 重新獲取定位
this.getLocation();
}
}
})
},
// 獲取位置
getLocation(){
uni.getLocation({
//type: 'wgs84',
type: 'gcj02',
success: (res)=>{
console.log(res);
this.doGetLocationAfter({
latitude : res.latitude,
longitude : res.longitude,
isOpenSetting : this.isOpenSetting,
})
},
fail: (res)=>{
console.log('用戶拒絕授權(quán)獲取位置信息,使用默認(rèn)經(jīng)緯度0 0');
this.doGetLocationAfter({
latitude : '',
longitude : '',
isOpenSetting : this.isOpenSetting,
})
// 根據(jù)位置數(shù)據(jù)更新頁面數(shù)據(jù)
},complete: (res)=>{
// console.log(res);
// 根據(jù)位置數(shù)據(jù)更新頁面數(shù)據(jù)
}
});
},
// 最終獲取到的信息數(shù)據(jù)
doGetLocationAfter(data){
console.log(data)
if(data.latitude != this.latitude || data.longitude != this.longitude){
this.latitude = data.latitude;
this.longitude = data.longitude;
// 根據(jù)位置數(shù)據(jù)更新頁面數(shù)據(jù)
}else{
console.log('位置信息無變化');
}
// 在這里處理最終獲取到的信息數(shù)據(jù)
},
// ......
}uniapp API文檔
獲取定位:
uni.getLocation(OBJECT) 獲取當(dāng)前的地理位置、速度
https://uniapp.dcloud.net.cn/api/location/location.html#getlocation
獲取用戶當(dāng)前的授權(quán)狀態(tài):
uni.getSetting(OBJECT) 獲取用戶的當(dāng)前設(shè)置。
https://uniapp.dcloud.net.cn/api/other/setting.html#getsetting
打開設(shè)置界面:
uni.openSetting(OBJECT) 調(diào)起客戶端小程序設(shè)置界面,返回用戶設(shè)置的操作結(jié)果。
https://uniapp.dcloud.net.cn/api/other/setting.html#opensetting
到此這篇關(guān)于uniapp中獲取位置信息處理的文章就介紹到這了,更多相關(guān)uniapp中獲取位置信息內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
javascript移動(dòng)設(shè)備Web開發(fā)中對touch事件的封裝實(shí)例
這篇文章主要介紹了javascript移動(dòng)設(shè)備Web開發(fā)中對touch事件的封裝實(shí)例,分別對tap事件、doubleTap事件、longTap事件、swipe事件做了封裝,需要的朋友可以參考下2014-06-06
TypeScript聲明文件的實(shí)現(xiàn)示例
TypeScript聲明文件是一種包含TypeScript類型信息的.d.ts文件,它允許開發(fā)者為已存在的JavaScript代碼提供類型注解,這些文件不包含可執(zhí)行代碼,下面就來詳細(xì)的介紹一下TypeScript聲明文件的實(shí)現(xiàn),感興趣的可以了解一下2025-12-12
微信小程序 組件的外部樣式externalClasses使用詳解
這篇文章主要介紹了微信小程序里 組件的外部樣式externalClasses使用詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-09-09
JS實(shí)現(xiàn)將數(shù)字金額轉(zhuǎn)換為大寫人民幣漢字的方法
這篇文章主要介紹了JS實(shí)現(xiàn)將數(shù)字金額轉(zhuǎn)換為大寫人民幣漢字的方法,涉及javascript字符串與數(shù)組操作的相關(guān)技巧,需要的朋友可以參考下2016-08-08
JavaScript監(jiān)聽DOM尺寸變化的解決方案
文章分析了傳統(tǒng)resize監(jiān)聽的弊端,如粒度粗糙、性能問題等并提出了多種解決方案,通過插入<object>、監(jiān)聽scroll或使用現(xiàn)代的ResizeObserver API等來實(shí)現(xiàn)了更精準(zhǔn)、高效地的元素尺寸監(jiān)聽,文章還討論了這些方法的優(yōu)缺點(diǎn)及適用場景,需要的朋友可以參考下2026-05-05
純js實(shí)現(xiàn)瀑布流布局及ajax動(dòng)態(tài)新增數(shù)據(jù)
這篇文章主要介紹了基于javascript實(shí)現(xiàn)瀑布流布局,及ajax動(dòng)態(tài)新增數(shù)據(jù)的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-04-04

