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
+26 -106
View File
@@ -20,7 +20,7 @@
:items="district"
></CitySelect>
</view>
<view class="iconfont icon-dizhi font-color-red" @click="getCurAddress()"></view>
<view class="iconfont icon-dizhi font-color-red" @click="getCurAddressFn()"></view>
</view>
</view>
<view class="item acea-row row-between-wrapper" style="flex-wrap: nowrap;">
@@ -75,7 +75,7 @@
<script type="text/babel">
import CitySelect from '@/components/CitySelect'
import config from '@/utils/mapConfig'
import {getCurAddress, getLocation} from "@/utils/common"
import { getAddress, getCity, postAddress, analysisAddress } from '@/api/user'
import attrs, { chs_extphone, required } from '@/utils/validate'
import { validatorDefaultCatch } from '@/utils/dialog'
@@ -192,113 +192,33 @@ export default {
})
},
//获取当前定位地址
getCurAddress() {
var that = this;
//定位
async getCurAddressFn() {
const that = this
uni.showLoading({
title: '定位中...'
});
//this.getLocation();
uni.getLocation({
type: 'wgs84',
success: function (res) {
let latitude = res.latitude;
let longitude = res.longitude;
console.log('latitude:' + latitude + 'longitude:' + longitude)
// #ifdef H5
Vue.jsonp(
'https://apis.map.qq.com/ws/geocoder/v1/?location=' + latitude + ',' + longitude + '&key=' + config.key,
{
//callbackName: 'QQmap',
output: 'jsonp',
}).then(json => {
// Success.
uni.hideLoading();
that.addressText =
json.result.ad_info.province + " " + json.result.ad_info.city + " " + json.result.ad_info.district;
that.address.province = json.result.ad_info.province;
that.address.city = json.result.ad_info.city;
that.address.district = json.result.ad_info.district;
that.$set(that.userAddress, 'detail', json.result.formatted_addresses.recommend);
//that.userAddress.detail = json.result.formatted_addresses.recommend;
// that.$refs.cityselect.adjustDefaultValue();
//设置city_id
var pIdx = that.getIndex(that.address.province, that.district);
if (pIdx > -1) {
var citys = that.district[pIdx].c;
var cIdx = that.getIndex(that.address.city, citys);
if (cIdx > -1) {
var city = citys[cIdx];
that.address.city_id = city.v;
console.log('city_id:' + city.v);
}
})
const map = await getLocation()
if (map.length > 1) {
const {latitude, longitude} = map[1]
getCurAddress(latitude, longitude, address => {
that.addressText = address.province + " " + address.city + " " + address.county
that.address.province = address.province
that.address.city = address.city
that.address.district = address.county
that.$set(that.userAddress, 'detail', address.formattedAddress)
// 设置city_id
const pIdx = that.getIndex(that.address.province, that.district)
if (pIdx > -1) {
const citys = that.district[pIdx].c
const cIdx = that.getIndex(that.address.city, citys)
if (cIdx > -1) {
const city = citys[cIdx]
that.address.city_id = city.v
console.log('city_id:' + city.v)
}
}).catch(err => {
// Failed.
console.log('地址解析失败:' + JSON.stringify(err));
uni.hideLoading();
})
// #endif
// #ifndef H5
uni.request({
url: 'https://apis.map.qq.com/ws/geocoder/v1/?location=' + latitude + ',' + longitude + '&key=' + config.key,
success: function (res) {
// console.log('res:'+JSON.stringify(res.data));
uni.hideLoading();
that.addressText =
res.data.result.ad_info.province + " " + res.data.result.ad_info.city + " " + res.data.result.ad_info.district;
that.address.province = res.data.result.ad_info.province;
that.address.city = res.data.result.ad_info.city;
that.address.district = res.data.result.ad_info.district;
//that.userAddress.detail = res.data.result.formatted_addresses.recommend;
that.$set(that.userAddress, 'detail', res.data.result.formatted_addresses.recommend);
// that.$refs.cityselect.adjustDefaultValue();
//设置city_id
var pIdx = that.getIndex(that.address.province, that.district);
if (pIdx > -1) {
var citys = that.district[pIdx].c;
var cIdx = that.getIndex(that.address.city, citys);
if (cIdx > -1) {
var city = citys[cIdx];
that.address.city_id = city.v;
console.log('city_id:' + city.v);
}
}
},
fail: function (res) {
console.log('地址解析失败');
uni.hideLoading();
},
complete: function () {
}
});
// #endif
},
fail: function (res) {
uni.hideLoading();
// uni.showToast({
// title:'城市定位失败:'+JSON.stringify(res),
// icon:'none',
// duration:2000
// })
}
});
}
})
}
},
getCityList: function () {
let that = this;