2025-10-19 14:15:52 +02:00
|
|
|
import { currentTool, Tool } from '$lib/components/toolbar/tools';
|
|
|
|
|
import { gpxStatistics, slicedGPXStatistics } from '$lib/logic/statistics';
|
2025-02-02 11:17:22 +01:00
|
|
|
import mapboxgl from 'mapbox-gl';
|
|
|
|
|
import { get } from 'svelte/store';
|
2025-10-19 14:15:52 +02:00
|
|
|
import { map } from '$lib/components/map/map';
|
2024-06-10 13:21:53 +02:00
|
|
|
|
|
|
|
|
export class StartEndMarkers {
|
|
|
|
|
start: mapboxgl.Marker;
|
|
|
|
|
end: mapboxgl.Marker;
|
|
|
|
|
updateBinded: () => void = this.update.bind(this);
|
2024-07-13 11:42:21 +02:00
|
|
|
unsubscribes: (() => void)[] = [];
|
2024-06-10 13:21:53 +02:00
|
|
|
|
2025-10-19 14:15:52 +02:00
|
|
|
constructor() {
|
2024-06-10 13:21:53 +02:00
|
|
|
let startElement = document.createElement('div');
|
|
|
|
|
let endElement = document.createElement('div');
|
|
|
|
|
startElement.className = `h-4 w-4 rounded-full bg-green-500 border-2 border-white`;
|
|
|
|
|
endElement.className = `h-4 w-4 rounded-full border-2 border-white`;
|
2025-02-02 11:17:22 +01:00
|
|
|
endElement.style.background =
|
|
|
|
|
'repeating-conic-gradient(#fff 0 90deg, #000 0 180deg) 0 0/8px 8px round';
|
2024-06-10 13:21:53 +02:00
|
|
|
|
|
|
|
|
this.start = new mapboxgl.Marker({ element: startElement });
|
|
|
|
|
this.end = new mapboxgl.Marker({ element: endElement });
|
|
|
|
|
|
2024-07-13 11:42:21 +02:00
|
|
|
this.unsubscribes.push(gpxStatistics.subscribe(this.updateBinded));
|
|
|
|
|
this.unsubscribes.push(slicedGPXStatistics.subscribe(this.updateBinded));
|
|
|
|
|
this.unsubscribes.push(currentTool.subscribe(this.updateBinded));
|
2024-06-10 13:21:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
update() {
|
2025-10-19 14:15:52 +02:00
|
|
|
const map_ = get(map);
|
|
|
|
|
if (!map_) return;
|
|
|
|
|
|
|
|
|
|
const tool = get(currentTool);
|
|
|
|
|
const statistics = get(slicedGPXStatistics)?.[0] ?? get(gpxStatistics);
|
2024-06-10 20:03:57 +02:00
|
|
|
if (statistics.local.points.length > 0 && tool !== Tool.ROUTING) {
|
2025-10-19 14:15:52 +02:00
|
|
|
this.start.setLngLat(statistics.local.points[0].getCoordinates()).addTo(map_);
|
2025-02-02 11:17:22 +01:00
|
|
|
this.end
|
|
|
|
|
.setLngLat(
|
|
|
|
|
statistics.local.points[statistics.local.points.length - 1].getCoordinates()
|
|
|
|
|
)
|
2025-10-19 14:15:52 +02:00
|
|
|
.addTo(map_);
|
2024-06-10 13:21:53 +02:00
|
|
|
} else {
|
|
|
|
|
this.start.remove();
|
|
|
|
|
this.end.remove();
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-13 11:42:21 +02:00
|
|
|
|
|
|
|
|
remove() {
|
2025-02-02 11:17:22 +01:00
|
|
|
this.unsubscribes.forEach((unsubscribe) => unsubscribe());
|
2024-07-13 11:42:21 +02:00
|
|
|
|
|
|
|
|
this.start.remove();
|
|
|
|
|
this.end.remove();
|
|
|
|
|
}
|
2025-02-02 11:17:22 +01:00
|
|
|
}
|