正常開發(fā)中,我們通過uni.getLocation 只能得到經(jīng)緯度,微信又沒有給我們具體的地理位置,這個(gè)時(shí)候我們可以通過反編譯,來獲取詳細(xì)地址。操作如下
第一步:我們先去騰訊地圖申請key騰訊地圖?
在 控制臺==> 應(yīng)用管理 ==> 我的應(yīng)用 ==>創(chuàng)建應(yīng)用 ==> 添加key==> 除了必填的,勾選WebserviceAPI
第二步:下載微信小程序JavaScriptSDK
地址:小程序sdk?
第三步:配置安全域名設(shè)置
登錄微信公眾平->開發(fā)->開發(fā)設(shè)置->服務(wù)器域名->將https://apis.map.qq.com
填入request合法域名
?第四步: 把剛剛第二步下載的sdk.js放在一個(gè)目錄下,我這邊用的是壓縮的(min)
?第五步:在需要的頁面引入文章來源:http://www.zghlxwxcb.cn/news/detail-765358.html
var QQMapWX = require('@/utils/qqmap-wx-jssdk.min.js')
?然后這里有new 一個(gè)實(shí)例對象,這個(gè)對象身上有一個(gè)方法reverseGeocoder,然后把我們用uni.getLocation獲取到的經(jīng)緯度傳過去,就能得到詳細(xì)地址。話不多說,直接上代碼文章來源地址http://www.zghlxwxcb.cn/news/detail-765358.html
<script>
var QQMapWX = require('@/utils/qqmap-wx-jssdk.min.js')
export default {
data() {
return {
}
},
onLoad() {
this.getLocation()
},
onShow() {
},
onReady() {
},
methods: {
// 獲取當(dāng)前位置
getLocation(){
const _this = this
uni.authorize({
scope: 'scope.userLocation',
success() {
let location = {
longitude: 0,
latitude: 0,
province: "",
city: "",
area: "",
street: "",
address: "",
};
uni.getLocation({
type: 'gcj02',
geocode: true,
success: function(res) {
console.log(res,'獲取經(jīng)緯度');
uni.setStorageSync('latitude', _this.latitude)
uni.setStorageSync('longitude', _this.longitude)
location.longitude = res.longitude;
location.latitude = res.latitude;
const qqmapsdk = new QQMapWX({
key: '你在騰訊地圖申請的key' //申請的key
});
qqmapsdk.reverseGeocoder({
location,
success: function(res) {
console.log(res, '獲取地址');
let info = res.result;
location.province = info.address_component.province;
location.city = info.address_component.city;
location.area = info.address_component.district;
location.street = info.address_component.street;
location.address = info.address;
console.log(location, '地址');
},
});
},
fail: function(err) {
_this.$util.modal({
c: '獲取位置失敗,請重新進(jìn)入小程序并同意獲取位置',
}, () => wx.openSetting())
}
})
},
fail: () => {
this.tipsAddress()
}
})
},
tipsAddress () {
this.$util.showModal({
content: '為了正常使用,請授權(quán)「位置信息」- 「使用小程序時(shí)允許」',
showCancel: false,
}).then(() => {
wx.openSetting({
success: (res) => {
if (res.errMsg === 'openSetting:ok') {
console.log(res.authSetting, 'res');
if (!res.authSetting['scope.userLocation']) {
this.tipsAddress()
} else {
this.getLocation()
}
}
},
fail: () => {
this.tipsAddress()
}
})
})
},
},
}
</script>
到了這里,關(guān)于uniapp 開發(fā)微信小程序,獲取經(jīng)緯度轉(zhuǎn)化詳細(xì)地址的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!