caprigis/webgis/js/controllers/marker_controller.js
2024-12-02 10:11:04 +01:00

48 lines
1.1 KiB
JavaScript

import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
static targets = ['coords'];
END_ZOOM = 19;
mapAnimate = {
animate: true,
duration: 1,
easeLinearity: 0.25
};
go(event) {
let map = window.LMap;
const coords = event.currentTarget
.getAttribute('data-coords').split(' ');
map.setView(
coords,
this.END_ZOOM,
this.mapAnimate
);
let marker = this.getMarker(map, coords);
marker?.openTooltip();
}
/**
* @param {L.Map} map
* @param {Array<String>} coords
* @returns {L.Marker}
*/
getMarker(map, coords) {
let marker;
map.eachLayer(layer => {
if (layer instanceof L.Marker) {
const latLng = layer.getLatLng();
const {lat, lng} = latLng;
if (lat === Number(coords[0]) && lng === Number(coords[1])) {
marker = layer;
return;
}
}
});
return marker;
}
}