Skip to content

Commit

Permalink
updated the GoogleMap.svelte for dedupe, optional chaining
Browse files Browse the repository at this point in the history
  • Loading branch information
tarunsinghofficial committed Aug 14, 2024
1 parent 1eabe50 commit c3aa680
Showing 1 changed file with 11 additions and 21 deletions.
32 changes: 11 additions & 21 deletions src/components/map/GoogleMap.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -55,38 +55,30 @@
async function loadStopsAndAddMarkers(lat, lng) {
const json = await loadStopsForLocation(lat, lng);
const stops = json.data.list;
allStops = [...allStops, ...stops];
const newStops = json.data.list;
allStops = [...new Map([...allStops, ...newStops].map((stop) => [stop.id, stop])).values()];
clearAllMarkers();
if (showRoute && selectedRoute) {
const stopsToShow = allStops.filter((s) => s.routeIds.includes(selectedRoute.id));
stopsToShow.forEach((s) => addMarker(s));
} else {
stops.forEach((s) => addMarker(s));
newStops.forEach((s) => addMarker(s));
}
}
function clearAllMarkers() {
markers.forEach(({ marker, overlay, element }) => {
if (marker) {
marker.setMap(null);
}
marker?.setMap(null);
if (overlay) {
if (overlay.map) {
overlay.map = null;
}
if (overlay.draw) {
overlay.draw = () => {};
}
if (overlay.onRemove) {
overlay.onRemove();
}
}
if (element && element.parentNode) {
element.parentNode.removeChild(element);
overlay.setMap(null);
overlay.draw = () => {};
overlay.onRemove?.();
}
element?.parentNode?.removeChild(element);
});
markers = [];
}
Expand Down Expand Up @@ -147,9 +139,7 @@
this.getPanes().overlayMouseTarget.appendChild(container);
};
overlay.onRemove = function () {
if (container.parentNode) {
container.parentNode.removeChild(container);
}
container?.parentNode?.removeChild(container);
};
markers.push({ s, marker, overlay, element: container });
}
Expand Down

0 comments on commit c3aa680

Please sign in to comment.