diff --git a/src/utils/marks.ts b/src/utils/marks.ts index bd6b147..34d8fd0 100644 --- a/src/utils/marks.ts +++ b/src/utils/marks.ts @@ -1,29 +1,30 @@ -import { divIcon, LatLngLiteral, Layer, LayerGroup, Map, marker, Marker } from "leaflet"; -import { arrowClusterIcon, createArrow } from "~/utils/arrow"; +import { divIcon, LatLngLiteral, Layer, LayerGroup, Map, marker, Marker } from 'leaflet'; +import { arrowClusterIcon, createArrow } from '~/utils/arrow'; import { MarkerClusterGroup } from 'leaflet.markercluster/dist/leaflet.markercluster-src.js'; -import { allwaysPositiveAngleDeg, angleBetweenPoints, distKmHaversine } from "~/utils/geom"; +import { allwaysPositiveAngleDeg, angleBetweenPoints, distKmHaversine } from '~/utils/geom'; import classNames from 'classnames'; interface KmMarksOptions { - showMiddleMarkers: boolean, - showEndMarker: boolean, - kmMarksStep: number, + showMiddleMarkers: boolean; + showEndMarker: boolean; + kmMarksStep: number; } class KmMarksLayer extends LayerGroup { - constructor(latlngs?: LatLngLiteral[], options?: KmMarksOptions){ + constructor(latlngs?: LatLngLiteral[], options?: KmMarksOptions) { super(); this.options = { showMiddleMarkers: true, showEndMarker: true, - kmMarksStep: 10, + kmMarksStep: 20, ...(options || {}), } as KmMarksOptions; } setLatLngs = (latlngs: LatLngLiteral[]): void => { if (!this.map) return; + this.marksLayer.clearLayers(); this.endMarker.clearLayers(); @@ -36,7 +37,7 @@ class KmMarksLayer extends LayerGroup { }; drawMiddleMarkers = (latlngs: LatLngLiteral[]) => { - const kmMarks = {}; + const marks = []; let last_km_mark = 0; this.distance = latlngs.reduce((dist, current, index) => { @@ -51,20 +52,20 @@ class KmMarksLayer extends LayerGroup { if (rounded > last_km_mark) { const angle = angleBetweenPoints( this.map.latLngToContainerPoint(current), - this.map.latLngToContainerPoint(next), + this.map.latLngToContainerPoint(next) ); + // if segment is too long, we'll add multiple markers on it for (let i = 1; i <= count; i += 1) { - const step = last_km_mark + (i * this.options.kmMarksStep); + const step = last_km_mark + i * this.options.kmMarksStep; const shift = (step - dist) / diff; const coords = { - lat: current.lat - ((current.lat - next.lat) * shift), - lng: current.lng - ((current.lng - next.lng) * shift), + lat: current.lat - (current.lat - next.lat) * shift, + lng: current.lng - (current.lng - next.lng) * shift, }; - kmMarks[step] = { ...coords, angle }; - this.marksLayer.addLayer(this.createMiddleMarker(coords, angle, step)); + marks.push(this.createMiddleMarker(coords, angle, step)); } last_km_mark = rounded; @@ -72,49 +73,54 @@ class KmMarksLayer extends LayerGroup { return sum; }, 0); + + this.marksLayer.addLayers(marks); }; - createMiddleMarker = (latlng: LatLngLiteral, angle: number, distance: number): Marker => marker(latlng, { - draggable: false, - interactive: false, - icon: divIcon({ - html: ` -