fix(geo-map): duplicate tracks after refresh

This commit is contained in:
Elian Doran 2025-02-01 23:35:34 +02:00
parent c84737b90e
commit 65e593f3ff
No known key found for this signature in database

View File

@ -1,4 +1,4 @@
import { Marker, type LatLng, type LeafletMouseEvent } from "leaflet"; import { GPX, Marker, type LatLng, type LeafletMouseEvent } from "leaflet";
import type FNote from "../../entities/fnote.js"; import type FNote from "../../entities/fnote.js";
import GeoMapWidget, { type InitCallback, type Leaflet } from "../geo_map.js"; import GeoMapWidget, { type InitCallback, type Leaflet } from "../geo_map.js";
import TypeWidget from "./type_widget.js" import TypeWidget from "./type_widget.js"
@ -91,8 +91,6 @@ interface CreateChildResponse {
} }
} }
type MarkerData = Record<string, Marker>;
enum State { enum State {
Normal, Normal,
NewNote NewNote
@ -103,7 +101,8 @@ export default class GeoMapTypeWidget extends TypeWidget {
private geoMapWidget: GeoMapWidget; private geoMapWidget: GeoMapWidget;
private _state: State; private _state: State;
private L!: Leaflet; private L!: Leaflet;
private currentMarkerData: MarkerData; private currentMarkerData: Record<string, Marker>;
private currentTrackData: Record<string, GPX>;
private gpxLoaded?: boolean; private gpxLoaded?: boolean;
static getType() { static getType() {
@ -115,6 +114,7 @@ export default class GeoMapTypeWidget extends TypeWidget {
this.geoMapWidget = new GeoMapWidget("type", (L: Leaflet) => this.#onMapInitialized(L)); this.geoMapWidget = new GeoMapWidget("type", (L: Leaflet) => this.#onMapInitialized(L));
this.currentMarkerData = {}; this.currentMarkerData = {};
this.currentTrackData = {};
this._state = State.Normal; this._state = State.Normal;
this.child(this.geoMapWidget); this.child(this.geoMapWidget);
@ -169,6 +169,11 @@ export default class GeoMapTypeWidget extends TypeWidget {
marker.remove(); marker.remove();
} }
// Delete all existing tracks
for (const track of Object.values(this.currentTrackData)) {
track.remove();
}
// Add the new markers. // Add the new markers.
this.currentMarkerData = {}; this.currentMarkerData = {};
const childNotes = await this.note.getChildNotes(); const childNotes = await this.note.getChildNotes();
@ -203,6 +208,7 @@ export default class GeoMapTypeWidget extends TypeWidget {
}); });
track.addTo(this.geoMapWidget.map); track.addTo(this.geoMapWidget.map);
this.currentTrackData[note.noteId] = track;
} }
#processNoteWithMarker(note: FNote, latLng: string) { #processNoteWithMarker(note: FNote, latLng: string) {