Skip to content

Commit 8cc0eb1

Browse files
Copilothustcc
andauthored
fix: custom SVG arrow markers, remove showDir, fix Zones/Heatmap/Layers label overlap
- map.tsx: replace AMap showDir with custom SVG arrow markers drawn at each segment midpoint using createElementNS (avoids innerHTML XSS). Arrow triangles are rotated to the bearing of each route segment and respond to color/opacity prop changes. Also removes showDir from setOptions effect since that option is no longer used. - zones-example / heatmap-home-example / layers-home-example: change label="" so ExampleCard does not render its own top-left badge, which was overlapping the custom info panels already present in each card. (Same pattern used by AnalyticsExample.) Agent-Logs-Url: https://github.com/hustcc/amapcn/sessions/079ab8f8-8473-4390-a820-1b08345751c3 Co-authored-by: hustcc <7856674+hustcc@users.noreply.github.com>
1 parent dc6649c commit 8cc0eb1

4 files changed

Lines changed: 48 additions & 5 deletions

File tree

‎map/src/map.tsx‎

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,7 @@ function MapRoute({
910910
const { map, AMap, isLoaded } = useMap();
911911
const polylineRef = useRef<AMapInstance>(null);
912912
const animMarkerRef = useRef<AMapInstance>(null);
913+
const arrowMarkersRef = useRef<AMapInstance[]>([]);
913914

914915
// Keep latest onClick in a ref to avoid stale closures
915916
const onClickRef = useRef(onClick);
@@ -926,7 +927,6 @@ function MapRoute({
926927
strokeOpacity: opacity,
927928
lineJoin: "round",
928929
lineCap: "round",
929-
showDir: arrows,
930930
strokeStyle: dashed ? "dashed" : "solid",
931931
strokeDasharray: dashed ? [10, 5] : undefined,
932932
});
@@ -1011,12 +1011,55 @@ function MapRoute({
10111011
strokeColor: color,
10121012
strokeWeight: width,
10131013
strokeOpacity: opacity,
1014-
showDir: arrows,
10151014
strokeStyle: dashed ? "dashed" : "solid",
10161015
strokeDasharray: dashed ? [10, 5] : undefined,
10171016
});
10181017
}, [color, width, opacity, arrows, dashed]);
10191018

1019+
// Direction arrow markers rendered as SVG triangles at each segment midpoint
1020+
useEffect(() => {
1021+
// Clean up previous markers first
1022+
arrowMarkersRef.current.forEach((m) => m.setMap(null));
1023+
arrowMarkersRef.current = [];
1024+
1025+
if (!arrows || !isLoaded || !map || !AMap || coordinates.length < 2) return;
1026+
1027+
for (let i = 0; i < coordinates.length - 1; i++) {
1028+
const [x1, y1] = coordinates[i];
1029+
const [x2, y2] = coordinates[i + 1];
1030+
const mx = (x1 + x2) / 2;
1031+
const my = (y1 + y2) / 2;
1032+
// Bearing from north, clockwise (matches CSS rotate direction)
1033+
const angleDeg = Math.atan2(x2 - x1, y2 - y1) * (180 / Math.PI);
1034+
const el = document.createElement("div");
1035+
const svgNS = "http://www.w3.org/2000/svg";
1036+
const svg = document.createElementNS(svgNS, "svg");
1037+
svg.setAttribute("width", "12");
1038+
svg.setAttribute("height", "12");
1039+
svg.setAttribute("viewBox", "0 0 12 12");
1040+
const poly = document.createElementNS(svgNS, "polygon");
1041+
poly.setAttribute("transform", `rotate(${angleDeg},6,6)`);
1042+
poly.setAttribute("points", "6,1 11,11 6,8 1,11");
1043+
poly.setAttribute("fill", color);
1044+
poly.setAttribute("opacity", String(opacity));
1045+
svg.appendChild(poly);
1046+
el.appendChild(svg);
1047+
const marker = new AMap.Marker({
1048+
position: [mx, my],
1049+
content: el,
1050+
offset: new AMap.Pixel(-6, -6),
1051+
});
1052+
marker.setMap(map);
1053+
arrowMarkersRef.current.push(marker);
1054+
}
1055+
1056+
return () => {
1057+
arrowMarkersRef.current.forEach((m) => m.setMap(null));
1058+
arrowMarkersRef.current = [];
1059+
};
1060+
// eslint-disable-next-line react-hooks/exhaustive-deps
1061+
}, [isLoaded, map, AMap, arrows, coordinates, color, opacity]);
1062+
10201063
return null;
10211064
}
10221065

‎site/src/app/(home)/_components/examples/heatmap-home-example.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const densityData = [
2121

2222
export function HeatmapHomeExample() {
2323
return (
24-
<ExampleCard label="Heatmap" className="aspect-square" delay="delay-900">
24+
<ExampleCard label="" className="aspect-square" delay="delay-900">
2525
<div className="absolute top-3 left-3 z-10 bg-background/95 backdrop-blur-md rounded-lg px-3 py-2 border border-border/50 shadow-lg">
2626
<div className="text-[10px] uppercase tracking-wider text-muted-foreground mb-1.5">
2727
Activity density

‎site/src/app/(home)/_components/examples/layers-home-example.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export function LayersHomeExample() {
1010
const [mode, setMode] = useState<LayerMode>("satellite");
1111

1212
return (
13-
<ExampleCard label="Layers" className="aspect-[2/1]" delay="delay-1000">
13+
<ExampleCard label="" className="aspect-[2/1]" delay="delay-1000">
1414
<div className="absolute top-3 left-3 z-10 bg-background/95 backdrop-blur-md rounded-lg px-3 py-2 border border-border/50 shadow-lg">
1515
<div className="text-[10px] uppercase tracking-wider text-muted-foreground mb-1.5">
1616
Tile layers

‎site/src/app/(home)/_components/examples/zones-example.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export function ZonesExample() {
2525
const [hover, setHover] = useState<string | null>(null);
2626

2727
return (
28-
<ExampleCard label="Zones" className="aspect-square" delay="delay-800">
28+
<ExampleCard label="" className="aspect-square" delay="delay-800">
2929
<div className="absolute top-3 left-3 z-10 bg-background/95 backdrop-blur-md rounded-lg px-3 py-2 border border-border/50 shadow-lg space-y-1.5">
3030
<div className="flex items-center gap-1.5">
3131
<Shield className="size-3 text-blue-500" />

0 commit comments

Comments
 (0)