Browse Source

'调用地图获取地址'

master
Lj 4 weeks ago
parent
commit
af7a920683
1 changed files with 186 additions and 7 deletions
  1. +186
    -7
      pages/subcomponent/select.vue

+ 186
- 7
pages/subcomponent/select.vue View File

@ -64,7 +64,7 @@
<text class="label">手机号</text> <text class="label">手机号</text>
<input class="input" v-model="form.phone" placeholder="请输入" /> <input class="input" v-model="form.phone" placeholder="请输入" />
</view> </view>
<view class="form-item" @tap="showRegionPicker = true">
<view class="form-item" @tap="selectAddress">
<text class="label">所在地区</text> <text class="label">所在地区</text>
<view class="input region-input"> <view class="input region-input">
<text :class="{placeholder: !form.address}"> <text :class="{placeholder: !form.address}">
@ -85,7 +85,10 @@
<view v-if="showRegionPicker" class="region-modal"> <view v-if="showRegionPicker" class="region-modal">
<view class="modal-header"> <view class="modal-header">
<text class="close-btn" @tap="showRegionPicker = false">关闭</text> <text class="close-btn" @tap="showRegionPicker = false">关闭</text>
<text class="modal-title">所在地区</text>
<text class="modal-title">手动选择地区</text>
</view>
<view class="region-tip">
<text>请手动选择准确的省市区信息</text>
</view> </view>
<view class="region-picker"> <view class="region-picker">
<picker-view style="height:300px;" :value="regionIndex" @change="onRegionChange"> <picker-view style="height:300px;" :value="regionIndex" @change="onRegionChange">
@ -161,7 +164,9 @@ export default {
phone: '', phone: '',
region: [], region: [],
address: '', address: '',
addressDetails: ''
addressDetails: '',
latitude: '',
longitude: ''
}, },
provinces: regionData, provinces: regionData,
cities: [], cities: [],
@ -256,7 +261,9 @@ export default {
region: [], region: [],
address: item.address, address: item.address,
addressDetails: item.addressDetails, addressDetails: item.addressDetails,
defaultFlag: item.defaultFlag
defaultFlag: item.defaultFlag,
latitude: item.latitude || '',
longitude: item.longitude || ''
} }
this.regionIndex = [0, 0, 0] this.regionIndex = [0, 0, 0]
this.cities = this.provinces[0]?.children || [] this.cities = this.provinces[0]?.children || []
@ -270,7 +277,9 @@ export default {
phone: '', phone: '',
region: [], region: [],
address: '', address: '',
addressDetails: ''
addressDetails: '',
latitude: '',
longitude: ''
} }
this.regionIndex = [0, 0, 0] this.regionIndex = [0, 0, 0]
this.cities = this.provinces[0]?.children || [] this.cities = this.provinces[0]?.children || []
@ -303,7 +312,9 @@ export default {
phone: this.form.phone, phone: this.form.phone,
address: this.form.address, address: this.form.address,
addressDetails: this.form.addressDetails, addressDetails: this.form.addressDetails,
defaultFlag: this.form.defaultFlag
defaultFlag: this.form.defaultFlag,
latitude: this.form.latitude,
longitude: this.form.longitude
} }
if (this.form.id) params.id = this.form.id if (this.form.id) params.id = this.form.id
this.$api('saveOrUpdateAddress', params, (res) => { this.$api('saveOrUpdateAddress', params, (res) => {
@ -325,6 +336,164 @@ export default {
} }
this.regionIndex = [pIdx, cIdx, dIdx] this.regionIndex = [pIdx, cIdx, dIdx]
}, },
//
selectAddress() {
//
uni.getSetting({
success: (res) => {
if (res.authSetting['scope.userLocation'] === false) {
//
uni.showModal({
title: '位置权限',
content: '需要获取您的位置信息来选择地址,请在设置中开启位置权限',
confirmText: '去设置',
success: (modalRes) => {
if (modalRes.confirm) {
uni.openSetting()
} else {
//
this.showRegionPicker = true
}
},
fail: () => {
this.showRegionPicker = true
}
})
return
}
//
uni.chooseLocation({
success: res => {
console.log(res);
this.form.latitude = res.latitude
this.form.longitude = res.longitude
var reg = /.+?(省|市|自治区|自治州|县|区)/g;
let address = ''
if (!res.address && res.name) { //
address = res.name
}
if (res.address || res.name) {
address = res.address + res.name
}
if(!address){
return
}
let arr = address.match(reg)
//
if (!arr || arr.length < 2) {
//
uni.showToast({
title: '地址信息不完整,请手动选择',
icon: 'none'
})
this.showRegionPicker = true
return
}
const province = arr[0] || ''
const city = arr[1] || ''
const district = arr[2] || ''
let detail = district || city || province || ''
this.form.addressDetails = address.substring(address.indexOf(detail) + detail.length)
this.form.address = `${province}${city}${district}`
// region
this.matchRegionData(province, city, district)
},
fail(e) {
console.log("获取位置信息失败!", e)
//
if (e.errMsg && e.errMsg.includes('auth deny')) {
uni.showModal({
title: '位置权限',
content: '需要获取您的位置信息来选择地址,请允许位置权限',
confirmText: '重新授权',
success: (modalRes) => {
if (modalRes.confirm) {
uni.openSetting()
} else {
this.showRegionPicker = true
}
}
})
} else if (e.errMsg && e.errMsg.includes('cancel')) {
//
this.showRegionPicker = true
} else {
//
uni.showToast({
title: '获取位置失败,请手动选择',
icon: 'none'
})
this.showRegionPicker = true
}
}
})
},
fail: () => {
//
this.showRegionPicker = true
}
})
},
// region
matchRegionData(provinceName, cityName, districtName) {
//
const provinceIndex = this.provinces.findIndex(p =>
provinceName.includes(p.name) || p.name.includes(provinceName.replace(/省|市|自治区|自治州/g, ''))
)
if (provinceIndex === -1) {
this.regionIndex = [0, 0, 0]
return
}
this.cities = this.provinces[provinceIndex]?.children || []
//
const cityIndex = this.cities.findIndex(c =>
cityName.includes(c.name) || c.name.includes(cityName.replace(/市|县|区/g, ''))
)
if (cityIndex === -1) {
this.regionIndex = [provinceIndex, 0, 0]
this.districts = this.cities[0]?.children || []
return
}
this.districts = this.cities[cityIndex]?.children || []
//
const districtIndex = this.districts.findIndex(d =>
districtName.includes(d.name) || d.name.includes(districtName.replace(/县|区/g, ''))
)
this.regionIndex = [
provinceIndex,
cityIndex,
districtIndex > -1 ? districtIndex : 0
]
// form.region
this.form.region = [
this.provinces[provinceIndex]?.code || '',
this.cities[cityIndex]?.code || '',
this.districts[districtIndex > -1 ? districtIndex : 0]?.code || ''
]
},
confirmRegion() { confirmRegion() {
const province = this.provinces[this.regionIndex[0]] const province = this.provinces[this.regionIndex[0]]
const city = this.cities[this.regionIndex[1]] const city = this.cities[this.regionIndex[1]]
@ -583,8 +752,18 @@ export default {
.arrow { color: #ccc; font-size: 28rpx; margin-left: 10rpx; } .arrow { color: #ccc; font-size: 28rpx; margin-left: 10rpx; }
} }
} }
.region-tip {
padding: 0 32rpx 16rpx 32rpx;
text-align: center;
text {
font-size: 26rpx;
color: #999;
}
}
.region-picker { .region-picker {
padding: 32rpx 0 0 0;
padding: 16rpx 0 0 0;
.picker-view { .picker-view {
width: 100%; height: 300rpx; display: flex; justify-content: center; align-items: center; width: 100%; height: 300rpx; display: flex; justify-content: center; align-items: center;
.active { color: #222; font-weight: bold; } .active { color: #222; font-weight: bold; }


Loading…
Cancel
Save