Skip to content

Commit f899bf1

Browse files
Copilothustcc
andauthored
fix: CSS triangle arrows (replaces SVG), try-catch heatmap/layer cleanup
- MapRoute arrows: replace SVG createElementNS markers with CSS border-trick triangle markers. The SVG namespace approach was not rendering inside AMap.Marker content; CSS border triangles work universally. Color set via CSS custom property (--ac) set through style.setProperty to keep CodeQL clean. - MapHeatmap, MapTrafficLayer, MapSatelliteLayer: wrap setMap(null) in try-catch during effect cleanup. AMap's internal getStatus() call inside setMap(null) throws when the map container is being torn down on page navigation. Swallowing the error prevents the app crash (TypeError: Cannot read properties of undefined (reading 'getStatus')). Agent-Logs-Url: https://github.com/hustcc/amapcn/sessions/69b547dd-ab30-4879-a41a-a63f12ee0876 Co-authored-by: hustcc <7856674+hustcc@users.noreply.github.com>
1 parent 8cc0eb1 commit f899bf1

1 file changed

Lines changed: 45 additions & 20 deletions

File tree

‎map/src/map.tsx‎

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,9 @@ function MapRoute({
10161016
});
10171017
}, [color, width, opacity, arrows, dashed]);
10181018

1019-
// Direction arrow markers rendered as SVG triangles at each segment midpoint
1019+
// Direction arrow markers rendered as CSS triangles at each segment midpoint.
1020+
// CSS border-trick triangles are used because SVG elements created via
1021+
// createElementNS may not render reliably inside AMap.Marker content.
10201022
useEffect(() => {
10211023
// Clean up previous markers first
10221024
arrowMarkersRef.current.forEach((m) => m.setMap(null));
@@ -1029,25 +1031,33 @@ function MapRoute({
10291031
const [x2, y2] = coordinates[i + 1];
10301032
const mx = (x1 + x2) / 2;
10311033
const my = (y1 + y2) / 2;
1032-
// Bearing from north, clockwise (matches CSS rotate direction)
1034+
// Compass bearing from north, clockwise
10331035
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);
1036+
// Wrapper div rotated to the route bearing; 16×14 px encloses the triangle
1037+
const wrapper = document.createElement("div");
1038+
wrapper.style.width = "16px";
1039+
wrapper.style.height = "14px";
1040+
wrapper.style.overflow = "visible";
1041+
wrapper.style.transform = `rotate(${angleDeg}deg)`;
1042+
wrapper.style.opacity = String(opacity);
1043+
// Set color as a CSS custom property so no user data reaches innerHTML
1044+
wrapper.style.setProperty("--ac", color);
1045+
// CSS upward-pointing triangle (tip at top = north at 0° rotation)
1046+
const tri = document.createElement("div");
1047+
tri.style.position = "absolute";
1048+
tri.style.left = "8px"; // center horizontally in the 16 px wrapper
1049+
tri.style.top = "0";
1050+
tri.style.width = "0";
1051+
tri.style.height = "0";
1052+
tri.style.borderLeft = "8px solid transparent";
1053+
tri.style.borderRight = "8px solid transparent";
1054+
tri.style.borderBottom = "14px solid var(--ac)";
1055+
wrapper.appendChild(tri);
10471056
const marker = new AMap.Marker({
10481057
position: [mx, my],
1049-
content: el,
1050-
offset: new AMap.Pixel(-6, -6),
1058+
content: wrapper,
1059+
offset: new AMap.Pixel(-8, -7),
1060+
zIndex: 200,
10511061
});
10521062
marker.setMap(map);
10531063
arrowMarkersRef.current.push(marker);
@@ -1427,7 +1437,14 @@ function MapHeatmap({
14271437
return () => {
14281438
cancelled = true;
14291439
if (heatmapRef.current) {
1430-
heatmapRef.current.setMap(null);
1440+
try {
1441+
// AMap.HeatMap.setMap(null) may call getStatus() on an internal
1442+
// reference that is already undefined when the page navigates away.
1443+
// Swallow the error so the teardown doesn't crash the app.
1444+
heatmapRef.current.setMap(null);
1445+
} catch {
1446+
// ignore
1447+
}
14311448
heatmapRef.current = null;
14321449
}
14331450
};
@@ -1469,7 +1486,11 @@ function MapTrafficLayer({ visible = true, opacity = 1 }: MapTrafficLayerProps)
14691486

14701487
return () => {
14711488
if (layerRef.current) {
1472-
layerRef.current.setMap(null);
1489+
try {
1490+
layerRef.current.setMap(null);
1491+
} catch {
1492+
// ignore teardown errors
1493+
}
14731494
layerRef.current = null;
14741495
}
14751496
};
@@ -1515,7 +1536,11 @@ function MapSatelliteLayer({ visible = true, opacity = 1 }: MapSatelliteLayerPro
15151536

15161537
return () => {
15171538
if (layerRef.current) {
1518-
layerRef.current.setMap(null);
1539+
try {
1540+
layerRef.current.setMap(null);
1541+
} catch {
1542+
// ignore teardown errors
1543+
}
15191544
layerRef.current = null;
15201545
}
15211546
};

0 commit comments

Comments
 (0)