?使用uni.getLocation()先獲取到當前位置信息的經(jīng)緯度
H5端測試可以使用http,上線打包需要設置為https模式
谷歌瀏覽器可能會獲取不到任何信息,因為谷歌瀏覽器位置信息是連接谷歌服務器獲取的,國內(nèi)用戶可能獲取位置信息失敗
getCurrentlocation(){
let that = this
uni.getLocation({
type: 'wgs84',
isHighAccuracy: true,//開啟高精度定位
success(res) {
console.log('當前位置的經(jīng)度:' + res.longitude);
console.log('當前位置的緯度:' + res.latitude);
}
})
}
使用高德開發(fā)平臺注冊一個key
高德開發(fā)平臺:高德開放平臺 | 高德地圖API (amap.com)
?拿到key在manifest.json里進行配置
文章來源:http://www.zghlxwxcb.cn/news/detail-549013.html
?配置完成后使用高德的逆地理編碼對上面請求拿到的經(jīng)緯度進行解析文章來源地址http://www.zghlxwxcb.cn/news/detail-549013.html
turnAdrr(longitude, latitude) {
let that = this
let _key = '高德里你注冊的key'; //高德API key類型:web服務
uni.request({
method: 'GET',
url: 'https://restapi.amap.com/v3/geocode/regeo?parameters',
data: {
key: _key,
location: `${longitude},${latitude}`,
output: 'JSON',
},
success: (res) => {
console.log(res.data.regeocode.formatted_address);
//用戶所在的地理位置信息
},
fail: r => {
console.log(r);
}
});
}
App的話可以直接調(diào)用uni.getLocation()拿到用戶的所在位置,不需要進行解析,可以直接拿到
uni.getLocation({
type: 'gcj02', //app直接獲取地理位置信息要使用gcj02
geocode:true , //必須要將geocode配置為true
isHighAccuracy: true,
success(res) {
console.log(res.address)
},
fail(err){
console.log(err)
}
})
到了這里,關(guān)于uniapp h5獲取用戶地理位置信息(高德地圖)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!