diff --git a/website/src/lib/components/map/gpx-layer/gpx-layer.ts b/website/src/lib/components/map/gpx-layer/gpx-layer.ts index c70be90ae..c19d071b4 100644 --- a/website/src/lib/components/map/gpx-layer/gpx-layer.ts +++ b/website/src/lib/components/map/gpx-layer/gpx-layer.ts @@ -55,14 +55,18 @@ function decrementColor(color: string) { } } -function getSvgForSymbol(symbol: string | undefined, layerColor: string) { +export function getSvgForSymbol(symbol?: string | undefined, layerColor?: string | undefined) { let symbolSvg = symbol ? symbols[symbol]?.iconSvg : undefined; return ` - ${Square.replace('width="24"', 'width="12"') - .replace('height="24"', 'height="12"') - .replace('stroke="currentColor"', 'stroke="SteelBlue"') - .replace('stroke-width="2"', 'stroke-width="1.5" x="9.6" y="0.4"') - .replace('fill="none"', `fill="${layerColor}"`)} + ${ + layerColor + ? Square.replace('width="24"', 'width="12"') + .replace('height="24"', 'height="12"') + .replace('stroke="currentColor"', 'stroke="SteelBlue"') + .replace('stroke-width="2"', 'stroke-width="1.5" x="9.6" y="0.4"') + .replace('fill="none"', `fill="${layerColor}"`) + : '' + } ${MapPin.replace('width="24"', '') .replace('height="24"', '') .replace('stroke="currentColor"', '') diff --git a/website/src/lib/components/toolbar/tools/waypoint/Waypoint.svelte b/website/src/lib/components/toolbar/tools/waypoint/Waypoint.svelte index caf64c8a4..b804bc6de 100644 --- a/website/src/lib/components/toolbar/tools/waypoint/Waypoint.svelte +++ b/website/src/lib/components/toolbar/tools/waypoint/Waypoint.svelte @@ -16,6 +16,8 @@ import { fileActions } from '$lib/logic/file-actions'; import { map } from '$lib/components/map/map'; import { mapCursor, MapCursorState } from '$lib/logic/map-cursor'; + import mapboxgl from 'mapbox-gl'; + import { getSvgForSymbol } from '$lib/components/map/gpx-layer/gpx-layer'; let props: { class?: string; @@ -39,6 +41,21 @@ }) ); + let marker: mapboxgl.Marker | null = null; + + function reset() { + if ($selectedWaypoint) { + selectedWaypoint.reset(); + } else { + name = ''; + description = ''; + link = ''; + sym = ''; + longitude = 0; + latitude = 0; + } + } + $effect(() => { if ($selectedWaypoint) { const wpt = $selectedWaypoint[0]; @@ -54,14 +71,7 @@ latitude = parseFloat(wpt.getLatitude().toFixed(6)); }); } else { - untrack(() => { - name = ''; - description = ''; - link = ''; - sym = ''; - longitude = 0; - latitude = 0; - }); + untrack(reset); } }); @@ -92,7 +102,7 @@ : undefined ); - selectedWaypoint.reset(); + reset(); } function setCoordinates(e: any) { @@ -100,6 +110,37 @@ longitude = e.lngLat.lng.toFixed(6); } + $effect(() => { + if ($selectedWaypoint) { + if (marker) { + marker.remove(); + marker = null; + } + } else if (latitude != 0 || longitude != 0) { + if ($map) { + if (marker) { + marker.setLngLat([longitude, latitude]).getElement().innerHTML = + getSvgForSymbol(symbolKey); + } else { + let element = document.createElement('div'); + element.classList.add('w-8', 'h-8'); + element.innerHTML = getSvgForSymbol(symbolKey); + marker = new mapboxgl.Marker({ + element, + anchor: 'bottom', + }) + .setLngLat([longitude, latitude]) + .addTo($map); + } + } + } else { + if (marker) { + marker.remove(); + marker = null; + } + } + }); + onMount(() => { if ($map) { $map.on('click', setCoordinates); @@ -112,6 +153,10 @@ $map.off('click', setCoordinates); mapCursor.notify(MapCursorState.TOOL_WITH_CROSSHAIR, false); } + if (marker) { + marker.remove(); + marker = null; + } }); @@ -210,7 +255,7 @@ {i18n._('toolbar.waypoint.create')} {/if} -