Conf(地图):使用天地图定位获取省市区

This commit is contained in:
lifizer
2026-01-05 11:03:48 +08:00
parent f4fcc7e8f5
commit 1780c4ff0a
12 changed files with 115 additions and 364 deletions
+33 -5
View File
@@ -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) {
+1 -1
View File
@@ -2,6 +2,6 @@
module.exports = {
//key: 'LBZBZ-XAJRO-HKYWF-SOACS-HSTYQ-ZUF44'
key: 'DGTBZ-3MZCQ-LHY5C-2F6VG-BBY7F-C3FUJ'
key: '0328840afc957efec4551c87ade9a38e'
};