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(() => {