From 491bc187be3182a0d68aa1e34e82ef55b57e6a82 Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Fri, 25 Oct 2024 00:02:38 +0800 Subject: [PATCH] feat: accept custom element for addMarker() --- src/BaseRenderer.mjs | 8 +------- src/BasicLeafletRenderer.mjs | 16 ++++++++++++---- src/BasicMaplibreRenderer.mjs | 7 +++---- src/BasicOpenlayersRenderer.mjs | 8 ++++---- 4 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/BaseRenderer.mjs b/src/BaseRenderer.mjs index 5154545..0344081 100644 --- a/src/BaseRenderer.mjs +++ b/src/BaseRenderer.mjs @@ -43,13 +43,7 @@ export default class { layers = [] data = [] aliases = [] - svgForAnchor = { - html: ' ', - size: [20, 20], - anchor: [10, 10], - } - - svgForMarker = { + svgPin = { html: '', size: [27, 41], anchor: [13.5, 35.25], diff --git a/src/BasicLeafletRenderer.mjs b/src/BasicLeafletRenderer.mjs index c093311..382fc19 100644 --- a/src/BasicLeafletRenderer.mjs +++ b/src/BasicLeafletRenderer.mjs @@ -88,12 +88,20 @@ const Renderer = class extends defaultExport { } addMarker (config) { - const svg = config.type === 'circle' ? this.svgForAnchor : this.svgForMarker + const options = config.element + ? { + html: config.element, + iconSize: config.size, + iconAnchor: config.anchor, + } + : { + html: this.svgPin.html, + iconSize: this.svgPin.size, + iconAnchor: this.svgPin.anchor, + } const markerIcon = L.divIcon({ - html: svg.html, + ...options, className: 'marker', - iconSize: svg.size, - iconAnchor: svg.anchor, }) const xy = Array.from(config.xy).reverse() const marker = L.marker(xy, { icon: markerIcon }) diff --git a/src/BasicMaplibreRenderer.mjs b/src/BasicMaplibreRenderer.mjs index c2295c6..3ed88b5 100644 --- a/src/BasicMaplibreRenderer.mjs +++ b/src/BasicMaplibreRenderer.mjs @@ -118,11 +118,10 @@ const Renderer = class extends defaultExport { } addMarker (config) { - const options = config.type === 'circle' + const options = config.element ? { - element: new window.DOMParser() - .parseFromString(this.svgForAnchor.html, 'image/svg+xml') - .querySelector('svg'), + element: config.element, + anchor: config.type === 'pin' ? 'bottom' : 'center', } : {} const marker = new this.maplibregl.Marker(options) diff --git a/src/BasicOpenlayersRenderer.mjs b/src/BasicOpenlayersRenderer.mjs index 3ce15b4..6c6e4e6 100644 --- a/src/BasicOpenlayersRenderer.mjs +++ b/src/BasicOpenlayersRenderer.mjs @@ -171,14 +171,14 @@ const Renderer = class extends defaultExport { // Apply vector layer for markers onto map addMarker (config) { - const position = this.ol.proj.fromLonLat(config.xy, this.crs) - const element = document.createElement('div') - element.innerHTML = config.type === 'circle' ? this.svgForAnchor.html : this.svgForMarker.html + const element = config.element ?? document.createElement('div') + if (!element.children) element.innerHTML = this.svgPin + const position = this.ol.proj.fromLonLat(config.xy, this.crs) const overlay = new ol.Overlay({ element, position, - positioning: config.type === 'circle' ? 'center-center' : 'bottom-center', + positioning: config.type !== 'pin' ? 'center-center' : 'bottom-center', }) this.map.addOverlay(overlay)