配置获取地址0.5
This commit is contained in:
parent
ce5fd8f53c
commit
8d6e51af05
|
|
@ -42,8 +42,8 @@
|
||||||
<text class="field-label">地址</text>
|
<text class="field-label">地址</text>
|
||||||
<view class="address-selector" @click="selectAddress">
|
<view class="address-selector" @click="selectAddress">
|
||||||
<text class="address-text">{{ formData.address || '选择收货地址' }}</text>
|
<text class="address-text">{{ formData.address || '选择收货地址' }}</text>
|
||||||
<view class="map-icon-wrapper">
|
<view class="map-icon-wrapper" title="点击打开地图">
|
||||||
<text class="map-icon">📍</text>
|
<text class="map-icon">🗺️</text>
|
||||||
</view>
|
</view>
|
||||||
</view>
|
</view>
|
||||||
</view>
|
</view>
|
||||||
|
|
@ -54,7 +54,6 @@
|
||||||
<view class="location-content">
|
<view class="location-content">
|
||||||
<view class="location-header">
|
<view class="location-header">
|
||||||
<text class="location-title">当前定位</text>
|
<text class="location-title">当前定位</text>
|
||||||
<text class="location-company">{{ currentLocation.company }}</text>
|
|
||||||
</view>
|
</view>
|
||||||
<text class="location-address">{{ currentLocation.address }}</text>
|
<text class="location-address">{{ currentLocation.address }}</text>
|
||||||
</view>
|
</view>
|
||||||
|
|
@ -164,61 +163,106 @@ export default {
|
||||||
|
|
||||||
const location = await getLocationWithPermission()
|
const location = await getLocationWithPermission()
|
||||||
console.log('位置信息:', location)
|
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) {
|
} catch (err) {
|
||||||
uni.hideLoading()
|
uni.hideLoading()
|
||||||
handleLocationError(err)
|
handleLocationError(err)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
// 处理位置信息
|
selectAddress() {
|
||||||
reverseGeocode(latitude, longitude) {
|
// 如果有当前定位信息,打开地图
|
||||||
uni.hideLoading()
|
if (this.currentLocation && this.currentLocation.latitude && this.currentLocation.longitude) {
|
||||||
|
this.openMap()
|
||||||
// 直接使用坐标信息,避免依赖外部API
|
} else {
|
||||||
this.currentLocation = {
|
// 没有定位信息,先获取位置
|
||||||
company: '当前位置',
|
uni.showModal({
|
||||||
address: `纬度: ${latitude.toFixed(6)}, 经度: ${longitude.toFixed(6)}`,
|
title: '提示',
|
||||||
latitude: latitude,
|
content: '需要先获取当前位置才能打开地图,是否现在获取?',
|
||||||
longitude: longitude,
|
|
||||||
}
|
|
||||||
|
|
||||||
// 尝试使用uni-app的地理编码(如果平台支持)
|
|
||||||
// #ifdef APP-PLUS || MP-WEIXIN
|
|
||||||
uni.reverseGeocoder({
|
|
||||||
location: {
|
|
||||||
latitude: latitude,
|
|
||||||
longitude: longitude,
|
|
||||||
},
|
|
||||||
success: res => {
|
success: res => {
|
||||||
console.log('逆地理编码结果:', res)
|
if (res.confirm) {
|
||||||
if (res.result) {
|
this.getCurrentLocation()
|
||||||
this.currentLocation = {
|
}
|
||||||
company: res.result.addressComponent?.city || '当前位置',
|
},
|
||||||
address:
|
})
|
||||||
res.result.formatted_addresses?.recommend || res.result.address || '未知地址',
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
// 打开地图
|
||||||
|
openMap() {
|
||||||
|
const { latitude, longitude } = this.currentLocation
|
||||||
|
|
||||||
|
// 显示加载提示
|
||||||
|
uni.showLoading({
|
||||||
|
title: '打开地图中...',
|
||||||
|
})
|
||||||
|
|
||||||
|
// 尝试打开系统地图应用
|
||||||
|
// #ifdef APP-PLUS
|
||||||
|
uni.openLocation({
|
||||||
latitude: latitude,
|
latitude: latitude,
|
||||||
longitude: longitude,
|
longitude: longitude,
|
||||||
}
|
name: '当前位置',
|
||||||
}
|
address: this.currentLocation.address || '未知地址',
|
||||||
|
scale: 18,
|
||||||
|
success: () => {
|
||||||
|
uni.hideLoading()
|
||||||
|
console.log('地图打开成功')
|
||||||
},
|
},
|
||||||
fail: err => {
|
fail: err => {
|
||||||
console.log('逆地理编码不可用,使用坐标信息')
|
uni.hideLoading()
|
||||||
|
console.error('打开地图失败:', err)
|
||||||
|
this.showMapInApp()
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
// #endif
|
// #endif
|
||||||
|
|
||||||
uni.showToast({
|
// #ifdef MP-WEIXIN
|
||||||
title: '位置获取成功',
|
uni.openLocation({
|
||||||
icon: 'success',
|
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() {
|
// 在应用内显示地图(备用方案)
|
||||||
// 选择地址逻辑
|
showMapInApp() {
|
||||||
uni.showToast({
|
uni.showModal({
|
||||||
title: '选择地址功能',
|
title: '地图功能',
|
||||||
icon: 'none',
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
this.formData.address = this.currentLocation.company + ' ' + this.currentLocation.address
|
this.formData.address = this.currentLocation.address
|
||||||
uni.showToast({
|
uni.showToast({
|
||||||
title: '已使用当前定位',
|
title: '已使用当前定位',
|
||||||
icon: 'success',
|
icon: 'success',
|
||||||
|
|
@ -413,13 +457,20 @@ export default {
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
width: 40rpx;
|
width: 48rpx;
|
||||||
height: 40rpx;
|
height: 48rpx;
|
||||||
background: #f15a04;
|
background: #f15a04;
|
||||||
border-radius: 50%;
|
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 {
|
.map-icon {
|
||||||
font-size: 24rpx;
|
font-size: 28rpx;
|
||||||
color: white;
|
color: white;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -95,12 +95,15 @@ export function handleLocationError(err) {
|
||||||
let errorMsg = '获取位置失败'
|
let errorMsg = '获取位置失败'
|
||||||
let showModal = false
|
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 = '定位权限被拒绝,请在设置中开启'
|
errorMsg = '定位权限被拒绝,请在设置中开启'
|
||||||
showModal = true
|
showModal = true
|
||||||
} else if (err.errMsg.includes('timeout')) {
|
} else if (errMsg.includes('timeout')) {
|
||||||
errorMsg = '定位超时,请重试'
|
errorMsg = '定位超时,请重试'
|
||||||
} else if (err.errMsg.includes('unsupported')) {
|
} else if (errMsg.includes('unsupported')) {
|
||||||
errorMsg = '当前设备不支持定位功能'
|
errorMsg = '当前设备不支持定位功能'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user