const mapInstance = $.xutil.xMapApi.instance("[data-name='map1']");
const mapLeaflet = mapInstance.leafletMap();
mapLeaflet.on('click', handleMapClick);
function handleMapClick(event) {
const current = mapInstance.selectedGeometry();
mapInstance.selectGeometry({type: current.type, points: current.points.slice(-1) });
const markers = getAllMarkers(this);
showLocations(markers);
showXY(markers);
}
function getAllMarkers(leafletMap) {
const allMarkers = [];
Object.values(leafletMap._layers).forEach(layer => {
if (layer instanceof L.Marker) {
allMarkers.push(layer);
}
});
return allMarkers;
}
function showXY(markers) {
const { lat, lng } = markers[0].getLatLng();
$('.tfLng').val(lng);
$('.tfLat').val(lat);
}
function showLoader() {
$('<div>', {'class':'loaderWrapper'})
.append($('<div>', {'class':'loader' }))
.appendTo('body');
}
function hideLoader(){
$('.loaderWrapper').remove();
}
async function showLocations(markers) {
showLoader();
const zoom = mapInstance.leafletMap().getZoom();
for (const marker of markers) {
const { lat, lng } = marker.getLatLng();
try {
const response = await $.getJSON({
url: 'https://nominatim.openstreetmap.org/reverse',
data: {
format: 'json',
addressdetails: '1',
zoom: zoom,
lat: lat,
lon: lng
}
});
marker.bindPopup(response.display_name).openPopup();
} catch (error) {
console.error('Fehler beim Abrufen der Adresse:', error);
}
}
hideLoader();
}