I have a map implemented via leaflet and there is a button to send the bounds of the map to a server which in turn responds to me with an array of elements. Each element has a latitude, a longitude and a unique id.
What I get from the server looks roughly like this:
[{
"poi_marker": "redMarker.png",
"poi_id": 412, //this is my unique ID
"poi_pos": {
"lat": 17.53243,
"lng": 14.52353
}
},...]
So far my code looked like this:
function createAndAddMarker(prefill) {
let marker = createMarker(prefill.poi_pos, prefill.poi_marker);
map.addLayer(marker);
marker._icon.dataset.uniqueId = prefill.poi_id;
return marker;
}
function createMarker(coords, iconUrl) {
let redMarker = L.icon({
iconUrl: iconUrl,
iconSize: [20, 32], // size of the icon
iconAnchor: [10, 32], // point of the icon which will correspond to marker's location
});
let createdMarker = new L.marker(coords, {
draggable: true,
zIndexOffset: 101,
icon: redMarker
});
return createdMarker;
}
and that worked fine! Now that I tried to implement leaflet.markercluster the first function looks pretty similar:
function createAndAddMarker(prefill) {
let marker = createMarker(prefill.poi_pos, prefill.poi_marker);
clusterLayer.addLayer(marker); //clusterLayer = layer that got created & added a when map did
marker._icon.dataset.uniqueId = prefill.poi_id;
return marker;
}
but there is already 2 problems with it.
Not all the markers get actually drawn to the map because some are already clustered. For these items that don't get drawn, the line
marker._icon.dataset.uniqueId = prefill.poi_id
will throw an error as it can not find "marker._icon" because the marker doesn't seem to have an icon as long as it's not drawn.The markers that get drawn get the uniqueId written onto them, but if I zoom out and make them cluster up too, the original marker+icon gets removed from the DOM and readded when I zoom in again. Unfortunately, it get's readded without my unique-id, so I have no idea how to persistenly link the marker with the unique Id.
I need to get corresponding unique-ID to a marker, every time I click on it, but have no idea how to achieve this.
Your input is very much appreciated!
If something is unclear pls let me know and I will attempt to clarify as much as possible.
Thanx in advance!