Conf(地图):使用天地图定位获取省市区
This commit is contained in:
+33
-5
@@ -20,11 +20,39 @@ module.exports = {
|
||||
})
|
||||
},
|
||||
|
||||
getCurAddress(latitude, longitude) {
|
||||
return uni.request({
|
||||
url: 'https://apis.map.qq.com/ws/geocoder/v1/?location=' + latitude + ',' + longitude +
|
||||
'&key=' + config.key
|
||||
});
|
||||
getCurAddress(latitude, longitude, cb) {
|
||||
const postStr = {
|
||||
lon: longitude,
|
||||
lat: latitude,
|
||||
ver: 1 // 接口版本号
|
||||
}
|
||||
const encodedPostStr = encodeURIComponent(JSON.stringify(postStr))
|
||||
|
||||
uni.request({
|
||||
url: `https://api.tianditu.gov.cn/geocoder?postStr=${encodedPostStr}&type=geocode&tk=${config.key}`,
|
||||
method: 'GET',
|
||||
success: (res) => {
|
||||
if (res.data.status === '0') { // 请求成功[6](@ref)
|
||||
const result = res.data.result;
|
||||
// 从返回数据中提取省市区信息[1,6](@ref)
|
||||
const addressComponent = result.addressComponent;
|
||||
const province = addressComponent.province; // 省份
|
||||
const city = addressComponent.city; // 城市
|
||||
const county = addressComponent.county; // 区县
|
||||
const formattedAddress = result.formatted_address; // 完整地址
|
||||
cb && cb({
|
||||
province,
|
||||
city,
|
||||
county,
|
||||
formattedAddress
|
||||
})
|
||||
}
|
||||
uni.hideLoading()
|
||||
},
|
||||
fail: () => {
|
||||
uni.hideLoading()
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
getLocationSetting(cb) {
|
||||
|
||||
Reference in New Issue
Block a user