From 173315bfbb7c6e897dec08d5bd8c01552ddae867 Mon Sep 17 00:00:00 2001 From: Inga Date: Thu, 16 Nov 2023 18:10:29 +0000 Subject: [PATCH] draw markers and connecting lines --- src/routePlanner/map.tsx | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/routePlanner/map.tsx b/src/routePlanner/map.tsx index 594fb39..6cd6145 100644 --- a/src/routePlanner/map.tsx +++ b/src/routePlanner/map.tsx @@ -4,7 +4,7 @@ import { InternalMapProps } from './types'; import 'leaflet/dist/leaflet.css'; -export const MapComponent = ({ onMapClick }: InternalMapProps) => { +export const MapComponent = ({ markers, onMapClick }: InternalMapProps) => { const mapContainerRef = useRef(null); const mapRef = useRef(undefined); @@ -46,5 +46,42 @@ export const MapComponent = ({ onMapClick }: InternalMapProps) => { }; }, [mapRef, onMapClick]); + useEffect(() => { + const map = mapRef.current; + if (!map) { + return; + } + + const layers: leaflet.Layer[] = []; + for (let i = 0; i < markers.length; i++) { + const thisMarker = markers[i]; + const previousMarker = markers[i - 1]; + if (thisMarker) { + if (previousMarker) { + layers.push( + leaflet.polyline([ + previousMarker.coordinates, + thisMarker.coordinates, + ]), + ); + } + layers.push( + leaflet.marker(thisMarker.coordinates, { + title: `${i + 1}`, + }), + ); + } + } + for (const layer of layers) { + layer.addTo(map); + } + + return () => { + for (const layer of layers) { + layer.removeFrom(map); + } + }; + }, [mapRef, markers]); + return
; };