From 9e99f1336ce7bf6eaaccb9322f637403aef5c6f7 Mon Sep 17 00:00:00 2001 From: wangzeyan <258785420@qq.com> Date: Sun, 18 May 2025 14:18:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=9C=B0=E5=9B=BEbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- admin/src/components/TencentMapPicker.vue | 44 ++++++++++++++++++----- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/admin/src/components/TencentMapPicker.vue b/admin/src/components/TencentMapPicker.vue index 89894672..3a9654f8 100644 --- a/admin/src/components/TencentMapPicker.vue +++ b/admin/src/components/TencentMapPicker.vue @@ -344,15 +344,34 @@ const initMap = () => { } const TMap = (window as any).TMap - const center = new TMap.LatLng(20.029077, 110.366367) + let center + + // 优先使用传入的坐标 + if (props.modelValue.lat && props.modelValue.lng) { + center = new TMap.LatLng(props.modelValue.lat, props.modelValue.lng) + } else { + // 默认使用北京坐标 + center = new TMap.LatLng(39.90403, 116.407526) + } + // 创建地图实例 map = new TMap.Map('container', { center, zoom: 12, }) + // 创建标记 marker = createMarker(map) + // 初始化时设置标记位置 + if (props.modelValue.lat && props.modelValue.lng) { + marker.updateGeometries({ + id: 'center', + position: new TMap.LatLng(props.modelValue.lat, props.modelValue.lng), + }) + } + + // 地图点击事件 map.on('click', (evt: any) => { map.setCenter(evt.latLng) marker.updateGeometries({ @@ -455,16 +474,23 @@ const handleError = (error: any) => { watch( () => props.modelValue, (newVal) => { - if (newVal.lat && newVal.lng && map) { - const latLng = new (window as any).TMap.LatLng(newVal.lat, newVal.lng) - map.setCenter(latLng) - marker?.updateGeometries({ - id: 'center', - position: latLng, - }) + if (newVal?.lat && newVal?.lng && map) { + try { + const latLng = new (window as any).TMap.LatLng(newVal.lat, newVal.lng) + map.setCenter(latLng) + marker?.updateGeometries({ + id: 'center', + position: latLng, + }) + } catch (error) { + console.warn('地图更新失败:', error) + } } }, - { immediate: true } + { + deep: true, + immediate: true, + } ) onBeforeUnmount(() => {