let aeMapInstance=null;
let aeMarkersByIndex={};
let aeInfoWindowInstance=null;
let aePendingOpenIdx=null;
window.initMap=async function (){
const { Map }=await google.maps.importLibrary("maps");
const { AdvancedMarkerElement }=await google.maps.importLibrary("marker");
const mapEl=document.getElementById("locations-acf-map");
if(!mapEl) return;
const bounds=new google.maps.LatLngBounds();
const locations=JSON.parse(document.getElementById("pjListingLocationsLatLng").innerHTML
);
const map=new Map(mapEl, {
disableDefaultUI: true,
zoom: 8,
mapId: "ef058da5131c63b69fa9f37a",
mapTypeId: "hybrid",
tilt: 0,
heading: 0
});
const infoWindow=new google.maps.InfoWindow();
aeMapInstance=map;
aeInfoWindowInstance=infoWindow;
aeMarkersByIndex={};
locations.forEach((position)=> {
const latLng={ lat: position.lat, lng: position.lng };
const tplDiv=document.getElementById('location-marker-' + position.idx);
const labelTxt=(tplDiv&&tplDiv.dataset.label)||String(position.idx);
const pin=document.createElement('div');
pin.className='ae-pin';
pin.setAttribute('role', 'button');
pin.setAttribute('aria-label', position.name + ' (stek ' + labelTxt + ')');
const num=document.createElement('span');
num.className='ae-pin__num';
num.textContent=labelTxt;
pin.appendChild(num);
const marker=new AdvancedMarkerElement({
map,
position: latLng,
title: position.name,
content: pin
});
marker.index=position.idx;
aeMarkersByIndex[String(position.idx)]={ marker, latLng };
bounds.extend(latLng);
marker.element.addEventListener("click", ()=> {
openMarker(position.idx);
});
});
map.fitBounds(bounds);
google.maps.event.addListenerOnce(map, "idle", ()=> {
map.setZoom(16);
if(aePendingOpenIdx!==null){
openMarker(aePendingOpenIdx);
aePendingOpenIdx=null;
}});
};
function openMarker(idx){
const key=String(idx);
if(!aeMapInstance||!aeInfoWindowInstance){
aePendingOpenIdx=idx;
return;
}
const entry=aeMarkersByIndex[key];
if(!entry) return;
const html=document.getElementById("location-marker-" + key);
if(html) aeInfoWindowInstance.setContent(html.innerHTML);
aeInfoWindowInstance.open({
anchor: entry.marker,
map: aeMapInstance
});
aeMapInstance.setCenter(entry.latLng);
aeMapInstance.setZoom(16);
}
window.aeOpenLocationMarker=openMarker;