Conf(地图):使用天地图定位获取省市区
This commit is contained in:
@@ -341,10 +341,11 @@ export default {
|
||||
},
|
||||
async getMapData() {
|
||||
const map = await getLocation()
|
||||
if (map) {
|
||||
if (map.length > 1) {
|
||||
const {latitude, longitude} = map[1]
|
||||
const address = await getCurAddress(latitude, longitude)
|
||||
this.location = address[1].data.result.ad_info.city
|
||||
getCurAddress(latitude, longitude, address => {
|
||||
this.location = address.city
|
||||
})
|
||||
}
|
||||
},
|
||||
init() {
|
||||
|
||||
+11
-48
@@ -74,7 +74,7 @@
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import config from '@/utils/mapConfig'
|
||||
import { getCurAddress, getLocation } from "@/utils/common"
|
||||
import {
|
||||
getHotelTypeList,
|
||||
getHotelList
|
||||
@@ -124,56 +124,19 @@ export default {
|
||||
})
|
||||
},
|
||||
//获取当前定位地址
|
||||
getCurAddress() {
|
||||
async getCurAddressFn() {
|
||||
const that = this
|
||||
//定位
|
||||
uni.showLoading({
|
||||
title: '定位中...'
|
||||
})
|
||||
uni.getLocation({
|
||||
type: 'wgs84',
|
||||
success: function(res) {
|
||||
let latitude = res.latitude
|
||||
let longitude = res.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.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()
|
||||
}
|
||||
})
|
||||
const map = await getLocation()
|
||||
if (map.length > 1) {
|
||||
const {latitude, longitude} = map[1]
|
||||
getCurAddress(latitude, longitude, address => {
|
||||
that.cityName = address.city
|
||||
that.paramCity = address.city
|
||||
})
|
||||
}
|
||||
},
|
||||
fetchTypeList() {
|
||||
this.hotelList = []
|
||||
@@ -195,7 +158,7 @@ export default {
|
||||
this.fetchList()
|
||||
uni.removeStorageSync('cityName')
|
||||
} else {
|
||||
this.getCurAddress()
|
||||
this.getCurAddressFn()
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
@@ -457,13 +457,11 @@ export default {
|
||||
},
|
||||
async getMapData() {
|
||||
const map = await getLocation()
|
||||
if (map.length > 1) {
|
||||
if (map.length > 1 && !this.cityName) {
|
||||
const {latitude, longitude} = map[1]
|
||||
const address = await getCurAddress(latitude, longitude)
|
||||
if(!this.cityName) {
|
||||
this.cityName = address[1].data.result.ad_info.city
|
||||
}
|
||||
// this.search()
|
||||
getCurAddress(latitude, longitude, address => {
|
||||
this.cityName = address.city
|
||||
})
|
||||
}
|
||||
},
|
||||
tapType(index) {
|
||||
|
||||
@@ -1399,13 +1399,12 @@ export default {
|
||||
getLocationPromise().then(mapData => {
|
||||
if (mapData.errMsg) {
|
||||
const { latitude, longitude } = mapData
|
||||
getCurAddress(latitude, longitude).then(address => {
|
||||
this.cityName = address[1].data.result.ad_info.city
|
||||
getCurAddress(latitude, longitude, address => {
|
||||
console.log(address)
|
||||
this.cityName = address.city
|
||||
uni.setStorageSync('locationCityName', this.cityName)
|
||||
this.isLocating = false
|
||||
this.getPageData()
|
||||
}).catch(() => {
|
||||
this.getPageData()
|
||||
})
|
||||
} else {
|
||||
this.isLocating = false
|
||||
|
||||
@@ -901,17 +901,18 @@ export default {
|
||||
})
|
||||
},
|
||||
async getMapData() {
|
||||
let location = uni.getStorageSync('location')
|
||||
const location = uni.getStorageSync('location')
|
||||
if (location.length === 0) {
|
||||
const map = await getLocation()
|
||||
if (map.length > 1) {
|
||||
const {latitude, longitude} = map[1]
|
||||
let address = await getCurAddress(latitude, longitude)
|
||||
address = address[1].data.result.ad_info
|
||||
location = address.province + address.city + address.district
|
||||
getCurAddress(latitude, longitude, address => {
|
||||
this.postAddress = address.formattedAddress
|
||||
})
|
||||
}
|
||||
} else {
|
||||
this.postAddress = location
|
||||
}
|
||||
this.postAddress = location
|
||||
},
|
||||
// 获取当前位置经纬度
|
||||
handleLoacation(toast) {
|
||||
@@ -923,9 +924,9 @@ export default {
|
||||
const map = await getLocation();
|
||||
if (map.length > 1) {
|
||||
const {latitude, longitude} = map[1]
|
||||
let address = await getCurAddress(latitude, longitude)
|
||||
address = address[1].data.result.ad_info
|
||||
that.postAddress = address.province + address.city + address.district
|
||||
getCurAddress(latitude, longitude, address => {
|
||||
that.postAddress = address.formattedAddress
|
||||
})
|
||||
}
|
||||
},
|
||||
fail: err => {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -77,11 +77,15 @@ export default {
|
||||
},
|
||||
methods: {
|
||||
async getMapData() {
|
||||
const map = await getLocation();
|
||||
uni.showLoading({
|
||||
title: '定位中...'
|
||||
})
|
||||
const map = await getLocation()
|
||||
if (map.length > 1) {
|
||||
const {latitude, longitude} = map[1];
|
||||
const address = await getCurAddress(latitude, longitude);
|
||||
this.cityName = address[1].data.result['ad_info']['city']
|
||||
const {latitude, longitude} = map[1]
|
||||
getCurAddress(latitude, longitude, address => {
|
||||
this.cityName = address.city
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
@@ -140,14 +140,17 @@ export default {
|
||||
},
|
||||
|
||||
async getMapData() {
|
||||
const map = await getLocation();
|
||||
uni.showLoading({
|
||||
title: '定位中...'
|
||||
})
|
||||
const map = await getLocation()
|
||||
if (map.length > 1) {
|
||||
const {latitude, longitude} = map[1];
|
||||
const address = await getCurAddress(latitude, longitude);
|
||||
this.cityName = address[1].data.result['ad_info']['city']
|
||||
const {latitude, longitude} = map[1]
|
||||
getCurAddress(latitude, longitude, address => {
|
||||
this.cityName = address.city
|
||||
this.fetchList()
|
||||
})
|
||||
}
|
||||
|
||||
await this.fetchList()
|
||||
},
|
||||
|
||||
async fetchList() {
|
||||
|
||||
@@ -234,111 +234,6 @@ export default {
|
||||
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 () {
|
||||
let that = this;
|
||||
|
||||
+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) {
|
||||
|
||||
+1
-1
@@ -2,6 +2,6 @@
|
||||
module.exports = {
|
||||
|
||||
//key: 'LBZBZ-XAJRO-HKYWF-SOACS-HSTYQ-ZUF44'
|
||||
key: 'DGTBZ-3MZCQ-LHY5C-2F6VG-BBY7F-C3FUJ'
|
||||
key: '0328840afc957efec4551c87ade9a38e'
|
||||
|
||||
};
|
||||
|
||||
@@ -40,8 +40,7 @@
|
||||
<script>
|
||||
import {
|
||||
fetchSpecialty
|
||||
} from "@/api/shop";
|
||||
import config from '@/utils/mapConfig';
|
||||
} from "@/api/shop"
|
||||
|
||||
export default {
|
||||
data() {
|
||||
@@ -53,12 +52,12 @@ export default {
|
||||
}
|
||||
},
|
||||
onShow() {
|
||||
let memCityName = uni.getStorageSync("cityName")
|
||||
const memCityName = uni.getStorageSync("cityName")
|
||||
if (memCityName.length) {
|
||||
this.cityName = memCityName;
|
||||
this.page = 1;
|
||||
this.list = [];
|
||||
this.fetchList(this.cityName);
|
||||
this.cityName = memCityName
|
||||
this.page = 1
|
||||
this.list = []
|
||||
this.fetchList(this.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>
|
||||
|
||||
Reference in New Issue
Block a user