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
+4 -3
View File
@@ -341,10 +341,11 @@ export default {
}, },
async getMapData() { async getMapData() {
const map = await getLocation() const map = await getLocation()
if (map) { if (map.length > 1) {
const {latitude, longitude} = map[1] const {latitude, longitude} = map[1]
const address = await getCurAddress(latitude, longitude) getCurAddress(latitude, longitude, address => {
this.location = address[1].data.result.ad_info.city this.location = address.city
})
} }
}, },
init() { init() {
+11 -48
View File
@@ -74,7 +74,7 @@
</template> </template>
<script> <script>
import config from '@/utils/mapConfig' import { getCurAddress, getLocation } from "@/utils/common"
import { import {
getHotelTypeList, getHotelTypeList,
getHotelList getHotelList
@@ -124,56 +124,19 @@ export default {
}) })
}, },
//获取当前定位地址 //获取当前定位地址
getCurAddress() { async getCurAddressFn() {
const that = this const that = this
//定位
uni.showLoading({ uni.showLoading({
title: '定位中...' title: '定位中...'
}) })
uni.getLocation({ const map = await getLocation()
type: 'wgs84', if (map.length > 1) {
success: function(res) { const {latitude, longitude} = map[1]
let latitude = res.latitude getCurAddress(latitude, longitude, address => {
let longitude = res.longitude that.cityName = address.city
// #ifdef H5 that.paramCity = address.city
Vue.jsonp( })
'https://apis.map.qq.com/ws/geocoder/v1/?location=' + latitude + ',' + longitude + '&key=' + }
config.key, {
output: 'jsonp',
}).then(json => {
// Success.
uni.hideLoading();
that.cityName = json.result.ad_info.city
that.paramCity = json.result.ad_info.city
//定位成功刷新数据
that.fetchList()
}).catch(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) {
uni.hideLoading();
that.cityName = res.data.result.ad_info.city
that.paramCity = res.data.result.ad_info.city
//定位成功刷新数据
that.fetchList()
},
fail: function(res) {
uni.hideLoading()
}
})
// #endif
},
fail: function(res) {
uni.hideLoading()
}
})
}, },
fetchTypeList() { fetchTypeList() {
this.hotelList = [] this.hotelList = []
@@ -195,7 +158,7 @@ export default {
this.fetchList() this.fetchList()
uni.removeStorageSync('cityName') uni.removeStorageSync('cityName')
} else { } else {
this.getCurAddress() this.getCurAddressFn()
} }
} }
}) })
+4 -6
View File
@@ -457,13 +457,11 @@ export default {
}, },
async getMapData() { async getMapData() {
const map = await getLocation() const map = await getLocation()
if (map.length > 1) { if (map.length > 1 && !this.cityName) {
const {latitude, longitude} = map[1] const {latitude, longitude} = map[1]
const address = await getCurAddress(latitude, longitude) getCurAddress(latitude, longitude, address => {
if(!this.cityName) { this.cityName = address.city
this.cityName = address[1].data.result.ad_info.city })
}
// this.search()
} }
}, },
tapType(index) { tapType(index) {
+3 -4
View File
@@ -1399,13 +1399,12 @@ export default {
getLocationPromise().then(mapData => { getLocationPromise().then(mapData => {
if (mapData.errMsg) { if (mapData.errMsg) {
const { latitude, longitude } = mapData const { latitude, longitude } = mapData
getCurAddress(latitude, longitude).then(address => { getCurAddress(latitude, longitude, address => {
this.cityName = address[1].data.result.ad_info.city console.log(address)
this.cityName = address.city
uni.setStorageSync('locationCityName', this.cityName) uni.setStorageSync('locationCityName', this.cityName)
this.isLocating = false this.isLocating = false
this.getPageData() this.getPageData()
}).catch(() => {
this.getPageData()
}) })
} else { } else {
this.isLocating = false this.isLocating = false
+9 -8
View File
@@ -901,17 +901,18 @@ export default {
}) })
}, },
async getMapData() { async getMapData() {
let location = uni.getStorageSync('location') const location = uni.getStorageSync('location')
if (location.length === 0) { if (location.length === 0) {
const map = await getLocation() const map = await getLocation()
if (map.length > 1) { if (map.length > 1) {
const {latitude, longitude} = map[1] const {latitude, longitude} = map[1]
let address = await getCurAddress(latitude, longitude) getCurAddress(latitude, longitude, address => {
address = address[1].data.result.ad_info this.postAddress = address.formattedAddress
location = address.province + address.city + address.district })
} }
} else {
this.postAddress = location
} }
this.postAddress = location
}, },
// 获取当前位置经纬度 // 获取当前位置经纬度
handleLoacation(toast) { handleLoacation(toast) {
@@ -923,9 +924,9 @@ export default {
const map = await getLocation(); const map = await getLocation();
if (map.length > 1) { if (map.length > 1) {
const {latitude, longitude} = map[1] const {latitude, longitude} = map[1]
let address = await getCurAddress(latitude, longitude) getCurAddress(latitude, longitude, address => {
address = address[1].data.result.ad_info that.postAddress = address.formattedAddress
that.postAddress = address.province + address.city + address.district })
} }
}, },
fail: err => { fail: err => {
+26 -106
View File
@@ -20,7 +20,7 @@
:items="district" :items="district"
></CitySelect> ></CitySelect>
</view> </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> </view>
<view class="item acea-row row-between-wrapper" style="flex-wrap: nowrap;"> <view class="item acea-row row-between-wrapper" style="flex-wrap: nowrap;">
@@ -75,7 +75,7 @@
<script type="text/babel"> <script type="text/babel">
import CitySelect from '@/components/CitySelect' 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 { getAddress, getCity, postAddress, analysisAddress } from '@/api/user'
import attrs, { chs_extphone, required } from '@/utils/validate' import attrs, { chs_extphone, required } from '@/utils/validate'
import { validatorDefaultCatch } from '@/utils/dialog' import { validatorDefaultCatch } from '@/utils/dialog'
@@ -192,113 +192,33 @@ export default {
}) })
}, },
//获取当前定位地址 //获取当前定位地址
getCurAddress() { async getCurAddressFn() {
const that = this
var that = this;
//定位
uni.showLoading({ uni.showLoading({
title: '定位中...' title: '定位中...'
}); })
const map = await getLocation()
//this.getLocation(); if (map.length > 1) {
const {latitude, longitude} = map[1]
uni.getLocation({ getCurAddress(latitude, longitude, address => {
type: 'wgs84', that.addressText = address.province + " " + address.city + " " + address.county
success: function (res) { that.address.province = address.province
let latitude = res.latitude; that.address.city = address.city
let longitude = res.longitude; that.address.district = address.county
that.$set(that.userAddress, 'detail', address.formattedAddress)
console.log('latitude:' + latitude + 'longitude:' + longitude) // 设置city_id
const pIdx = that.getIndex(that.address.province, that.district)
// #ifdef H5 if (pIdx > -1) {
Vue.jsonp( const citys = that.district[pIdx].c
'https://apis.map.qq.com/ws/geocoder/v1/?location=' + latitude + ',' + longitude + '&key=' + config.key, const cIdx = that.getIndex(that.address.city, citys)
{ if (cIdx > -1) {
//callbackName: 'QQmap', const city = citys[cIdx]
output: 'jsonp', that.address.city_id = city.v
}).then(json => { console.log('city_id:' + city.v)
// 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);
}
} }
}
})
}).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 () { getCityList: function () {
let that = this; let that = this;
+8 -4
View File
@@ -77,11 +77,15 @@ export default {
}, },
methods: { methods: {
async getMapData() { async getMapData() {
const map = await getLocation(); uni.showLoading({
title: '定位中...'
})
const map = await getLocation()
if (map.length > 1) { if (map.length > 1) {
const {latitude, longitude} = map[1]; const {latitude, longitude} = map[1]
const address = await getCurAddress(latitude, longitude); getCurAddress(latitude, longitude, address => {
this.cityName = address[1].data.result['ad_info']['city'] this.cityName = address.city
})
} }
}, },
+9 -6
View File
@@ -140,14 +140,17 @@ export default {
}, },
async getMapData() { async getMapData() {
const map = await getLocation(); uni.showLoading({
title: '定位中...'
})
const map = await getLocation()
if (map.length > 1) { if (map.length > 1) {
const {latitude, longitude} = map[1]; const {latitude, longitude} = map[1]
const address = await getCurAddress(latitude, longitude); getCurAddress(latitude, longitude, address => {
this.cityName = address[1].data.result['ad_info']['city'] this.cityName = address.city
this.fetchList()
})
} }
await this.fetchList()
}, },
async fetchList() { async fetchList() {
@@ -234,111 +234,6 @@ export default {
duration: 2000 duration: 2000
}); });
} }
},
//获取当前定位地址
getCurAddress() {
var 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);
}
}
}).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 () { getCityList: function () {
let that = this; let that = this;
+33 -5
View File
@@ -20,11 +20,39 @@ module.exports = {
}) })
}, },
getCurAddress(latitude, longitude) { getCurAddress(latitude, longitude, cb) {
return uni.request({ const postStr = {
url: 'https://apis.map.qq.com/ws/geocoder/v1/?location=' + latitude + ',' + longitude + lon: longitude,
'&key=' + config.key 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) { getLocationSetting(cb) {
+1 -1
View File
@@ -2,6 +2,6 @@
module.exports = { module.exports = {
//key: 'LBZBZ-XAJRO-HKYWF-SOACS-HSTYQ-ZUF44' //key: 'LBZBZ-XAJRO-HKYWF-SOACS-HSTYQ-ZUF44'
key: 'DGTBZ-3MZCQ-LHY5C-2F6VG-BBY7F-C3FUJ' key: '0328840afc957efec4551c87ade9a38e'
}; };
+7 -68
View File
@@ -40,8 +40,7 @@
<script> <script>
import { import {
fetchSpecialty fetchSpecialty
} from "@/api/shop"; } from "@/api/shop"
import config from '@/utils/mapConfig';
export default { export default {
data() { data() {
@@ -53,12 +52,12 @@ export default {
} }
}, },
onShow() { onShow() {
let memCityName = uni.getStorageSync("cityName") const memCityName = uni.getStorageSync("cityName")
if (memCityName.length) { if (memCityName.length) {
this.cityName = memCityName; this.cityName = memCityName
this.page = 1; this.page = 1
this.list = []; this.list = []
this.fetchList(this.cityName); this.fetchList(this.cityName)
uni.removeStorageSync("cityName") uni.removeStorageSync("cityName")
} }
}, },
@@ -89,67 +88,7 @@ export default {
}) })
} }
}) })
}, }
//获取当前定位地址
getCurAddress() {
var that = this;
//定位
uni.showLoading({
title: '定位中...'
});
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, {
output: 'jsonp',
}).then(json => {
// Success.
uni.hideLoading();
that.cityName = json.result.ad_info.city;
//定位成功刷新数据
that.fetchList()
}).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) {
uni.hideLoading();
that.cityName = res.data.result.ad_info.city;
//定位成功刷新数据
that.fetchList()
},
fail: function (res) {
console.log('地址解析失败');
uni.hideLoading();
},
complete: function () {
}
});
// #endif
},
fail: function (res) {
uni.hideLoading();
}
});
},
} }
} }
</script> </script>