本篇文章分享一下我在實際開發(fā)小程序時遇到的需要獲取用戶當(dāng)前位置的問題,在小程序開發(fā)過程中經(jīng)常使用到的獲取定位功能。uniapp官方也提供了相應(yīng)的API供我們使用。 官網(wǎng)地址:uni.getLocation(OBJECT))
-
首先根據(jù)官網(wǎng)uni.getLocation(OBJECT))來獲取地理位置信息
uni.getLocation({
type: 'wgs84',
success: function (res) {
console.log('當(dāng)前位置的經(jīng)度:' + res.longitude);
console.log('當(dāng)前位置的緯度:' + res.latitude);
}
});
注意:這里面有個大坑(就是只會第一次授權(quán)彈出提示授權(quán)彈窗,加入拒絕授權(quán)后面不會在彈出)

-
其次配置manifest.json文件

"permission" : {
"scope.userLocation" : {
"desc" : "你的位置信息將用于小程序位置接口的效果展示"
}
},
"requiredPrivateInfos": ["getLocation", "chooseLocation"]
3.這樣就可以獲取到當(dāng)前位置信息了,效果圖如下

4.下面來解決上面那個大坑(如果用戶拒絕獲取用戶信息后,不能在彈出授權(quán)信息彈窗)
var _this=this
uni.authorize({
scope: 'scope.userLocation',
success() { //1.1 允許授權(quán)
_this.getLocation();
},
fail() { //1.2 拒絕授權(quán)
uni.showModal({
content: '檢測到您沒打開獲取信息功能權(quán)限,是否去設(shè)置打開?',
confirmText: "確認",
cancelText: '取消',
success: (res) => {
if (res.confirm) {
uni.openSetting({
success: (res) => {
console.log(res);
_this.getLocation();
}
})
} else {
console.log('取消');
return false;
}
}
})
return false;
}
})
A.用戶點擊拒絕后會彈出模態(tài)框,去調(diào)用設(shè)置界面文章來源:http://www.zghlxwxcb.cn/news/detail-584585.html

B.點擊確定即可跳轉(zhuǎn)設(shè)置界面修改允許權(quán)限文章來源地址http://www.zghlxwxcb.cn/news/detail-584585.html

這樣就可以了,下一篇文章將拿到的經(jīng)緯度換取中文詳細地址信息。
到了這里,關(guān)于uniapp獲取用戶當(dāng)前位置信息(第一節(jié))的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!