|
|
@@ -11,6 +11,7 @@ interface Props {
|
|
|
enableClickMark?: boolean;
|
|
|
securityJsCode?: string;
|
|
|
showSearch?: boolean; // 是否显示地址搜索框
|
|
|
+ initialMarker?: boolean; // 是否在初始化时添加标记点
|
|
|
}
|
|
|
|
|
|
const props = withDefaults(defineProps<Props>(), {
|
|
|
@@ -18,7 +19,8 @@ const props = withDefaults(defineProps<Props>(), {
|
|
|
height: "400px",
|
|
|
enableClickMark: false,
|
|
|
securityJsCode: "",
|
|
|
- showSearch: false
|
|
|
+ showSearch: false,
|
|
|
+ initialMarker: true // 默认添加初始标记点
|
|
|
});
|
|
|
|
|
|
const emit = defineEmits<{
|
|
|
@@ -84,49 +86,84 @@ const addMarker = (lng: number, lat: number) => {
|
|
|
map.setCenter([lng, lat]);
|
|
|
};
|
|
|
|
|
|
-// 添加自定义标记(支持点击事件和自定义颜色)
|
|
|
-const addCustomMarker = (lng: number, lat: number, title: string, onClick?: () => void, iconColor: string = 'red') => {
|
|
|
+// 添加自定义标记(支持悬停事件和自定义颜色)
|
|
|
+const addCustomMarker = (
|
|
|
+ lng: number,
|
|
|
+ lat: number,
|
|
|
+ title: string,
|
|
|
+ events?: {
|
|
|
+ onClick?: () => void;
|
|
|
+ onMouseEnter?: () => void;
|
|
|
+ onMouseLeave?: () => void;
|
|
|
+ },
|
|
|
+ iconColor: string = 'red'
|
|
|
+) => {
|
|
|
if (!map || !AMap) {
|
|
|
console.error('[addCustomMarker] map or AMap not initialized');
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
+ // 使用 HTML 内容创建红色标记点(更可靠)
|
|
|
+ let markerContent = '';
|
|
|
+ if (iconColor === 'red') {
|
|
|
+ markerContent = `<div style="
|
|
|
+ position: relative;
|
|
|
+ width: 25px;
|
|
|
+ height: 34px;
|
|
|
+ ">
|
|
|
+ <svg width="25" height="34" viewBox="0 0 25 34" xmlns="http://www.w3.org/2000/svg">
|
|
|
+ <path d="M12.5 0C5.6 0 0 5.6 0 12.5c0 9.4 12.5 21.5 12.5 21.5S25 21.9 25 12.5C25 5.6 19.4 0 12.5 0z" fill="#e74c3c"/>
|
|
|
+ <circle cx="12.5" cy="12" r="5" fill="white"/>
|
|
|
+ </svg>
|
|
|
+ </div>`;
|
|
|
+ }
|
|
|
+
|
|
|
// 创建标记点
|
|
|
const customMarker = new AMap.Marker({
|
|
|
position: [lng, lat],
|
|
|
title: title,
|
|
|
- cursor: "pointer", // 设置鼠标样式为 pointer
|
|
|
- animation: "AMAP_ANIMATION_DROP"
|
|
|
+ cursor: "pointer",
|
|
|
+ animation: "AMAP_ANIMATION_DROP",
|
|
|
+ content: markerContent || undefined
|
|
|
});
|
|
|
|
|
|
- // 设置红色图标(使用高德地图官方图标)
|
|
|
- if (iconColor === 'red') {
|
|
|
- try {
|
|
|
- const icon = new AMap.Icon({
|
|
|
- image: 'https://webapi.amap.com/theme/v1.3/markers/n_red_b.png',
|
|
|
- size: new AMap.Size(25, 34),
|
|
|
- imageSize: new AMap.Size(25, 34),
|
|
|
- anchor: 'bottom-center'
|
|
|
- });
|
|
|
- customMarker.setIcon(icon);
|
|
|
- console.log('[addCustomMarker] 红色图标设置成功');
|
|
|
- } catch (error) {
|
|
|
- console.error('[addCustomMarker] 设置红色图标失败:', error);
|
|
|
- }
|
|
|
- }
|
|
|
+ console.log('[addCustomMarker] 标记点创建成功:', { lng, lat, title, iconColor });
|
|
|
|
|
|
- // 添加点击事件
|
|
|
- if (onClick) {
|
|
|
- customMarker.on("click", onClick);
|
|
|
+ // 添加事件监听
|
|
|
+ if (events) {
|
|
|
+ if (events.onClick) {
|
|
|
+ customMarker.on("click", events.onClick);
|
|
|
+ }
|
|
|
+ if (events.onMouseEnter) {
|
|
|
+ customMarker.on("mouseover", events.onMouseEnter);
|
|
|
+ }
|
|
|
+ if (events.onMouseLeave) {
|
|
|
+ customMarker.on("mouseout", events.onMouseLeave);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// 添加到地图
|
|
|
map.add(customMarker);
|
|
|
|
|
|
- console.log('[addCustomMarker] 标记点添加成功:', { lng, lat, title });
|
|
|
return customMarker;
|
|
|
};
|
|
|
|
|
|
+// 获取经纬度对应的像素坐标
|
|
|
+const lngLatToPixel = (lng: number, lat: number): { x: number; y: number } | null => {
|
|
|
+ if (!map) {
|
|
|
+ console.error('[lngLatToPixel] 地图未初始化');
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ const pixel = map.lngLatToContainer([lng, lat]);
|
|
|
+ return { x: pixel.x, y: pixel.y };
|
|
|
+ } catch (error) {
|
|
|
+ console.error('[lngLatToPixel] 转换失败:', error);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
const reloadMap = () => {
|
|
|
if (map) {
|
|
|
map.destroy();
|
|
|
@@ -355,9 +392,12 @@ const initMap = async () => {
|
|
|
});
|
|
|
});
|
|
|
|
|
|
- if (hasValidCoords()) {
|
|
|
- console.log('[地图初始化] 添加标记点');
|
|
|
+ if (hasValidCoords() && props.initialMarker) {
|
|
|
+ console.log('[地图初始化] 添加初始标记点');
|
|
|
addMarker(props.longitude!, props.latitude!);
|
|
|
+ } else if (hasValidCoords()) {
|
|
|
+ console.log('[地图初始化] 只设置中心点,不添加标记点');
|
|
|
+ map.setCenter([props.longitude!, props.latitude!]);
|
|
|
} else if (props.showSearch) {
|
|
|
// 如果是新数据且显示搜索框,获取当前位置
|
|
|
console.log('[地图初始化] 无坐标数据,获取当前位置');
|
|
|
@@ -463,9 +503,13 @@ const loadAMap = () => {
|
|
|
watch(
|
|
|
() => [props.longitude, props.latitude],
|
|
|
([newLng, newLat]) => {
|
|
|
- if (newLng && newLat && map) {
|
|
|
+ // 只有在 initialMarker 为 true 时才自动添加标记点
|
|
|
+ if (newLng && newLat && map && props.initialMarker) {
|
|
|
addMarker(newLng as number, newLat as number);
|
|
|
map.setCenter([newLng, newLat]);
|
|
|
+ } else if (newLng && newLat && map) {
|
|
|
+ // 只更新中心点,不添加标记
|
|
|
+ map.setCenter([newLng, newLat]);
|
|
|
}
|
|
|
}
|
|
|
);
|
|
|
@@ -486,7 +530,8 @@ defineExpose({
|
|
|
reloadMap,
|
|
|
addMarker,
|
|
|
addCustomMarker, // 暴露添加自定义标记的方法
|
|
|
- searchAddress // 暴露搜索方法
|
|
|
+ searchAddress, // 暴露搜索方法
|
|
|
+ lngLatToPixel // 暴露坐标转换方法
|
|
|
});
|
|
|
</script>
|
|
|
|