配置获取地址0.5

This commit is contained in:
WindowBird 2025-08-19 09:27:16 +08:00
parent ce5fd8f53c
commit 8d6e51af05
2 changed files with 101 additions and 47 deletions

View File

@ -42,8 +42,8 @@
<text class="field-label">地址</text>
<view class="address-selector" @click="selectAddress">
<text class="address-text">{{ formData.address || '选择收货地址' }}</text>
<view class="map-icon-wrapper">
<text class="map-icon">📍</text>
<view class="map-icon-wrapper" title="点击打开地图">
<text class="map-icon">🗺</text>
</view>
</view>
</view>
@ -54,7 +54,6 @@
<view class="location-content">
<view class="location-header">
<text class="location-title">当前定位</text>
<text class="location-company">{{ currentLocation.company }}</text>
</view>
<text class="location-address">{{ currentLocation.address }}</text>
</view>
@ -164,61 +163,106 @@ export default {
const location = await getLocationWithPermission()
console.log('位置信息:', location)
this.reverseGeocode(location.latitude, location.longitude)
// 使
this.currentLocation = {
address: `纬度: ${location.latitude.toFixed(6)}, 经度: ${location.longitude.toFixed(6)}`,
latitude: location.latitude,
longitude: location.longitude,
}
uni.hideLoading()
uni.showToast({
title: '位置获取成功,点击地图图标查看',
icon: 'success',
duration: 3000,
})
} catch (err) {
uni.hideLoading()
handleLocationError(err)
}
},
//
reverseGeocode(latitude, longitude) {
uni.hideLoading()
selectAddress() {
//
if (this.currentLocation && this.currentLocation.latitude && this.currentLocation.longitude) {
this.openMap()
} else {
//
uni.showModal({
title: '提示',
content: '需要先获取当前位置才能打开地图,是否现在获取?',
success: res => {
if (res.confirm) {
this.getCurrentLocation()
}
},
})
}
},
// 使API
this.currentLocation = {
company: '当前位置',
address: `纬度: ${latitude.toFixed(6)}, 经度: ${longitude.toFixed(6)}`,
//
openMap() {
const { latitude, longitude } = this.currentLocation
//
uni.showLoading({
title: '打开地图中...',
})
//
// #ifdef APP-PLUS
uni.openLocation({
latitude: latitude,
longitude: longitude,
}
// 使uni-app
// #ifdef APP-PLUS || MP-WEIXIN
uni.reverseGeocoder({
location: {
latitude: latitude,
longitude: longitude,
},
success: res => {
console.log('逆地理编码结果:', res)
if (res.result) {
this.currentLocation = {
company: res.result.addressComponent?.city || '当前位置',
address:
res.result.formatted_addresses?.recommend || res.result.address || '未知地址',
latitude: latitude,
longitude: longitude,
}
}
name: '当前位置',
address: this.currentLocation.address || '未知地址',
scale: 18,
success: () => {
uni.hideLoading()
console.log('地图打开成功')
},
fail: err => {
console.log('逆地理编码不可用,使用坐标信息')
uni.hideLoading()
console.error('打开地图失败:', err)
this.showMapInApp()
},
})
// #endif
uni.showToast({
title: '位置获取成功',
icon: 'success',
// #ifdef MP-WEIXIN
uni.openLocation({
latitude: latitude,
longitude: longitude,
name: '当前位置',
address: this.currentLocation.address || '未知地址',
scale: 18,
success: () => {
uni.hideLoading()
},
fail: err => {
uni.hideLoading()
console.error('打开地图失败:', err)
this.showMapInApp()
},
})
// #endif
// #ifdef H5
// H5使线
uni.hideLoading()
const mapUrl = `https://uri.amap.com/marker?position=${longitude},${latitude}&name=${encodeURIComponent('当前位置')}&address=${encodeURIComponent(this.currentLocation.address || '未知地址')}`
window.open(mapUrl, '_blank')
// #endif
},
selectAddress() {
//
uni.showToast({
title: '选择地址功能',
icon: 'none',
//
showMapInApp() {
uni.showModal({
title: '地图功能',
content: `${this.currentLocation.address}\n\n经纬度${this.currentLocation.latitude.toFixed(6)}, ${this.currentLocation.longitude.toFixed(6)}`,
showCancel: false,
confirmText: '确定',
})
},
@ -228,7 +272,7 @@ export default {
return
}
this.formData.address = this.currentLocation.company + ' ' + this.currentLocation.address
this.formData.address = this.currentLocation.address
uni.showToast({
title: '已使用当前定位',
icon: 'success',
@ -413,13 +457,20 @@ export default {
display: flex;
align-items: center;
justify-content: center;
width: 40rpx;
height: 40rpx;
width: 48rpx;
height: 48rpx;
background: #f15a04;
border-radius: 50%;
transition: all 0.3s ease;
box-shadow: 0 4rpx 12rpx rgba(241, 90, 4, 0.3);
&:active {
transform: scale(0.9);
box-shadow: 0 2rpx 8rpx rgba(241, 90, 4, 0.4);
}
.map-icon {
font-size: 24rpx;
font-size: 28rpx;
color: white;
}
}

View File

@ -95,12 +95,15 @@ export function handleLocationError(err) {
let errorMsg = '获取位置失败'
let showModal = false
if (err.errMsg.includes('auth deny') || err.errMsg.includes('no permission')) {
// 安全地检查错误信息
const errMsg = err?.errMsg || err?.message || ''
if (errMsg.includes('auth deny') || errMsg.includes('no permission')) {
errorMsg = '定位权限被拒绝,请在设置中开启'
showModal = true
} else if (err.errMsg.includes('timeout')) {
} else if (errMsg.includes('timeout')) {
errorMsg = '定位超时,请重试'
} else if (err.errMsg.includes('unsupported')) {
} else if (errMsg.includes('unsupported')) {
errorMsg = '当前设备不支持定位功能'
}