From 6703a197fb090e2e252e7b9c22f12b728746ba90 Mon Sep 17 00:00:00 2001 From: itskovacs Date: Wed, 6 Aug 2025 21:11:19 +0200 Subject: [PATCH] :zap: Minor optimizations --- src/src/app/shared/map.ts | 65 ++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 35 deletions(-) diff --git a/src/src/app/shared/map.ts b/src/src/app/shared/map.ts index 21e461f..27caecf 100644 --- a/src/src/app/shared/map.ts +++ b/src/src/app/shared/map.ts @@ -3,6 +3,8 @@ import "leaflet.markercluster"; import "leaflet-contextmenu"; import { Place } from "../types/poi"; +export const DEFAULT_TILE_URL = + "https://{s}.basemaps.cartocdn.com/rastertiles/voyager/{z}/{x}/{y}{r}.png"; export interface ContextMenuItem { text: string; index?: number; @@ -19,41 +21,34 @@ export interface MarkerOptions extends L.MarkerOptions { } export function createMap( - contextMenuItems?: ContextMenuItem[], - tilelayer?: string, + contextMenuItems: ContextMenuItem[] = [], + tilelayer: string = DEFAULT_TILE_URL, ): L.Map { - let southWest = L.latLng(-89.99, -180); - let northEast = L.latLng(89.99, 180); - let bounds = L.latLngBounds(southWest, northEast); + const southWest = L.latLng(-89.99, -180); + const northEast = L.latLng(89.99, 180); + const bounds = L.latLngBounds(southWest, northEast); - let _contextMenuItems = contextMenuItems || []; - let map = L.map("map", { + const map = L.map("map", { maxBoundsViscosity: 1.0, zoomControl: false, contextmenu: true, - contextmenuItems: _contextMenuItems, + contextmenuItems: contextMenuItems, } as MapOptions) .setZoom(10) .setMaxBounds(bounds); - L.tileLayer( - tilelayer || - "https://{s}.basemaps.cartocdn.com/rastertiles/voyager/{z}/{x}/{y}{r}.png", - { - maxZoom: 17, - minZoom: 3, - attribution: - '© OpenStreetMap, © CARTO', - }, - ).addTo(map); + L.tileLayer(tilelayer, { + maxZoom: 17, + minZoom: 3, + attribution: + '© OpenStreetMap, © CARTO', + }).addTo(map); return map; } export function placeHoverTooltip(place: Place): string { - let content = `
${place.name}
`; - content += `
${place.category.name}
`; - return content; + return `
${place.name}
${place.category.name}
`.trim(); } export function createClusterGroup(): L.MarkerClusterGroup { @@ -62,7 +57,7 @@ export function createClusterGroup(): L.MarkerClusterGroup { disableClusteringAtZoom: 11, showCoverageOnHover: false, maxClusterRadius: 50, - iconCreateFunction: function (cluster) { + iconCreateFunction: (cluster) => { const count = cluster.getChildCount(); return L.divIcon({ html: `
${count}
`, @@ -85,7 +80,7 @@ export function tripDayMarker(item: { }), }); - let touchDevice = "ontouchstart" in window; + const touchDevice = "ontouchstart" in window; if (!touchDevice) { marker.bindTooltip( `
${item.text}
`, @@ -105,11 +100,9 @@ export function placeToMarker( grayscale: boolean = false, gpxInBubble: boolean = false, ): L.Marker { - let marker: L.Marker; - let options: any = { + const options: Partial = { riseOnHover: true, title: place.name, - place_id: place.id, alt: "", }; @@ -119,6 +112,7 @@ export function placeToMarker( let markerClasses = "w-full h-full rounded-full bg-center bg-cover bg-white"; if (grayscale) markerClasses += " grayscale"; + const iconHtml = `
@@ -127,17 +121,17 @@ export function placeToMarker( `; const icon = L.divIcon({ - html: iconHtml, + html: iconHtml.trim(), iconSize: [56, 56], className: "", }); - marker = new L.Marker([+place.lat, +place.lng], { + const marker = new L.Marker([+place.lat, +place.lng], { ...options, icon, }); - let touchDevice = "ontouchstart" in window; + const touchDevice = "ontouchstart" in window; if (!touchDevice) { marker.bindTooltip(placeHoverTooltip(place), { direction: "right", @@ -153,12 +147,13 @@ export function gpxToPolyline(gpx: string): L.Polyline { const gpxDoc = parser.parseFromString(gpx, "application/xml"); const trkpts = Array.from(gpxDoc.querySelectorAll("trkpt")); - const latlngs = trkpts.map((pt) => { - return [ - parseFloat(pt.getAttribute("lat")!), - parseFloat(pt.getAttribute("lon")!), - ] as [number, number]; - }); + const latlngs = trkpts.map( + (pt) => + [ + parseFloat(pt.getAttribute("lat")!), + parseFloat(pt.getAttribute("lon")!), + ] as [number, number], + ); return L.polyline(latlngs, { color: "blue" }); }