地图初步

main
Lufaneng 1 year ago
parent 2a02861d93
commit 2d9fa1d234

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.6 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.2 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

@ -13,13 +13,7 @@
style="width: 100%; height: 400px; border: 1px solid #ccc; position: relative; z-index: 1" style="width: 100%; height: 400px; border: 1px solid #ccc; position: relative; z-index: 1"
v-loading="mapLoading || isGettingLocation" v-loading="mapLoading || isGettingLocation"
:element-loading-text="isGettingLocation ? '正在获取当前位置...' : '正在加载地图...'" :element-loading-text="isGettingLocation ? '正在获取当前位置...' : '正在加载地图...'"
> ></div>
<!-- 自定义标记 -->
<div v-if="markerPosition && map" class="custom-marker" :style="markerPosition">
<div class="marker-pin"></div>
<div class="marker-shadow"></div>
</div>
</div>
<!-- 错误提示 --> <!-- 错误提示 -->
<div v-if="mapError" class="error-info"> <div v-if="mapError" class="error-info">
@ -30,13 +24,7 @@
<div v-else class="coordinate-info"> <div v-else class="coordinate-info">
<p>当前选择坐标经度 {{ selectedLng || '未选择' }}, 纬度 {{ selectedLat || '未选择' }}</p> <p>当前选择坐标经度 {{ selectedLng || '未选择' }}, 纬度 {{ selectedLat || '未选择' }}</p>
<p class="tip">点击地图选择坐标位置</p> <p class="tip">点击地图选择坐标位置</p>
<p <!-- <p v-if="selectedLng && selectedLat" class="selected-info"> </p> -->
v-if="selectedLng && selectedLat && !props.longitude && !props.latitude"
class="current-location-info"
>
📍 已自动定位到当前位置
</p>
<p v-else-if="selectedLng && selectedLat" class="selected-info"> 已选择坐标点</p>
</div> </div>
<!-- 使用原生按钮 --> <!-- 使用原生按钮 -->
@ -47,7 +35,7 @@
@click="getCurrentLocationManually" @click="getCurrentLocationManually"
:disabled="isGettingLocation" :disabled="isGettingLocation"
> >
{{ isGettingLocation ? '定位中...' : '📍 获取当前位置' }} {{ isGettingLocation ? '定位中...' : '📍 移至当前位置' }}
</button> </button>
<button type="button" class="cancel-btn" @click="handleClose"></button> <button type="button" class="cancel-btn" @click="handleClose"></button>
<button <button
@ -98,6 +86,7 @@ const isGettingLocation = ref(false)
let map = null let map = null
let isMapInitialized = false let isMapInitialized = false
let markerLayer = null
// //
const getCurrentLocation = () => { const getCurrentLocation = () => {
@ -138,7 +127,7 @@ const getCurrentLocation = () => {
enableHighAccuracy: true, enableHighAccuracy: true,
timeout: 10000, timeout: 10000,
maximumAge: 60000, maximumAge: 60000,
} },
) )
}).finally(() => { }).finally(() => {
isGettingLocation.value = false isGettingLocation.value = false
@ -167,29 +156,20 @@ const loadTencentMapAPI = () => {
}) })
} }
//
const latLngToPixel = (lat, lng) => {
if (!map) return null
try {
const point = map.projectToContainer(new window.TMap.LatLng(lat, lng))
return {
left: point.getX() + 'px',
top: point.getY() + 'px',
}
} catch (error) {
console.error('坐标转换失败:', error)
return null
}
}
// //
const updateMarkerPosition = () => { const updateMarkerPosition = () => {
if (selectedLng.value && selectedLat.value && map) { if (selectedLng.value && selectedLat.value && map) {
const position = latLngToPixel(selectedLat.value, selectedLng.value) markerLayer.setGeometries([
markerPosition.value = position {
} else { id: '1', //id
markerPosition.value = null styleId: 'myStyle', //id
position: new TMap.LatLng(selectedLat.value, selectedLng.value), //
properties: {
//
title: 'marker1',
},
},
])
} }
} }
@ -218,11 +198,11 @@ const initMap = async () => {
selectedLat.value = currentLocation.latitude selectedLat.value = currentLocation.latitude
selectedLng.value = currentLocation.longitude selectedLng.value = currentLocation.longitude
ElMessage.success('已获取到当前位置') // ElMessage.success('')
console.log('使用当前位置作为地图中心:', { centerLat, centerLng }) console.log('使用当前位置作为地图中心:', { centerLat, centerLng })
} catch (locationError) { } catch (locationError) {
console.warn('获取当前位置失败,使用默认位置:', locationError.message) console.warn('获取当前位置失败,使用默认位置:', locationError.message)
ElMessage.warning('获取当前位置失败,使用默认位置') // ElMessage.warning('使')
} }
} }
@ -232,6 +212,33 @@ const initMap = async () => {
zoom: MAP_CONFIG.DEFAULT_ZOOM, zoom: MAP_CONFIG.DEFAULT_ZOOM,
}) })
markerLayer = new TMap.MultiMarker({
map: map, //
//
styles: {
//styleId"myStyle"stylesstyleId
myStyle: new TMap.MarkerStyle({
width: 25, //
height: 35, //
// src: '@/assets/images/map_icon.png', //
//
anchor: { x: 16, y: 32 },
}),
},
//
geometries: [
{
id: '1', //id
styleId: 'myStyle', //id
position: new TMap.LatLng(centerLat, centerLng), //
properties: {
//
title: 'marker1',
},
},
],
})
// //
map.on('click', (event) => { map.on('click', (event) => {
const lat = event.latLng.getLat() const lat = event.latLng.getLat()
@ -246,10 +253,10 @@ const initMap = async () => {
updateMarkerPosition() updateMarkerPosition()
// //
map.setCenter(new window.TMap.LatLng(lat, lng)) // map.setCenter(new window.TMap.LatLng(lat, lng))
// //
ElMessage.success(`已选择坐标: ${lng.toFixed(6)}, ${lat.toFixed(6)}`) // ElMessage.success(`: ${lng.toFixed(6)}, ${lat.toFixed(6)}`)
}) })
// //
@ -278,7 +285,7 @@ const updateMapCenter = () => {
if (map && (props.longitude || props.latitude)) { if (map && (props.longitude || props.latitude)) {
const center = new window.TMap.LatLng( const center = new window.TMap.LatLng(
props.latitude || MAP_CONFIG.DEFAULT_CENTER.latitude, props.latitude || MAP_CONFIG.DEFAULT_CENTER.latitude,
props.longitude || MAP_CONFIG.DEFAULT_CENTER.longitude props.longitude || MAP_CONFIG.DEFAULT_CENTER.longitude,
) )
map.setCenter(center) map.setCenter(center)
@ -294,8 +301,8 @@ const getCurrentLocationManually = async () => {
try { try {
const currentLocation = await getCurrentLocation() const currentLocation = await getCurrentLocation()
selectedLat.value = currentLocation.latitude // selectedLat.value = currentLocation.latitude
selectedLng.value = currentLocation.longitude // selectedLng.value = currentLocation.longitude
// //
if (map) { if (map) {
@ -306,7 +313,7 @@ const getCurrentLocationManually = async () => {
}) })
} }
ElMessage.success('已获取到当前位置') // ElMessage.success('')
} catch (error) { } catch (error) {
console.error('手动获取位置失败:', error) console.error('手动获取位置失败:', error)
ElMessage.error('获取当前位置失败: ' + error.message) ElMessage.error('获取当前位置失败: ' + error.message)
@ -329,7 +336,7 @@ const confirmSelection = () => {
latitude: selectedLat.value, latitude: selectedLat.value,
}) })
ElMessage.success('坐标确认成功!') // ElMessage.success('')
handleClose() handleClose()
} else { } else {
console.log('坐标未选择,无法确认') console.log('坐标未选择,无法确认')
@ -351,7 +358,7 @@ const handleOpened = () => {
// //
const handleClose = () => { const handleClose = () => {
console.log('关闭对话框') // console.log('')
markerPosition.value = null markerPosition.value = null
visible.value = false visible.value = false
} }
@ -360,7 +367,7 @@ const handleClose = () => {
watch([() => props.longitude, () => props.latitude], ([newLng, newLat]) => { watch([() => props.longitude, () => props.latitude], ([newLng, newLat]) => {
selectedLng.value = newLng selectedLng.value = newLng
selectedLat.value = newLat selectedLat.value = newLat
updateMapCenter() // updateMapCenter()
}) })
// //
@ -519,4 +526,4 @@ onUnmounted(() => {
} }
} }
} }
</style> </style>

@ -65,8 +65,6 @@ const handleCoordinateConfirm = (coordinates) => {
console.log('接收到坐标确认事件:', coordinates) console.log('接收到坐标确认事件:', coordinates)
formField.value.longitude = coordinates.longitude formField.value.longitude = coordinates.longitude
formField.value.latitude = coordinates.latitude formField.value.latitude = coordinates.latitude
console.log('表单字段已更新:', formField.value)
ElMessage.success(`坐标选择成功: 经度 ${coordinates.longitude}, 纬度 ${coordinates.latitude}`)
} }
</script> </script>

Loading…
Cancel
Save