I am trying to create a markercluster that has markers with IDs, because I would later like to remove a single marker from the map. However, I'm having trouble even getting these labeled markers to appear on the map. I'm loading them from a geoJSON formatted variable individualPoints
. I can add these markers to the map directly with the following code snippet:
// create object to save markers
var markersID = {};
// exchanges latitude & longitude
function switchLatLong(oldLocation) {
return new L.LatLng(oldLocation[1], oldLocation[0]);
}
var geoJsonLayer = L.geoJson(individualPoints, {
onEachFeature: function(feature, layer) {
/* can add directly to the map--this works */
markersID[feature.properties.id] =
L.marker(switchLatLong(feature.geometry.coordinates))
.addTo(map);
}
});
But I want them to be in a cluster, so I think I need to tag each marker with an ID (as in this question), then add this group to the cluster as follows:
var geoJsonLayer = L.geoJson(individualPoints, {
onEachFeature: function(feature, layer) {
/* tag the marker with the id */
markersID[feature.properties.id] =
L.marker(switchLatLong(feature.geometry.coordinates));
}
});
// create a marker cluster group, and add the markers to this.
var markers = L.markerClusterGroup({});
/* here's where I'm trying to toggle to use the markers with ID numbers */
//markers.addLayer(geoJsonLayer); // this works
markers.addLayers(markersID); // this doesn't
Adding geoJsonLayer
works just fine, but adding markersID
doesn't. So I must be adding markersID
incorrectly.
Here's a jsfiddle.
https://jsfiddle.net/anfw0n6w/
Questions: Is there some better way to create markers that have an ID that I can refer to later? How else can I add markersID
to the markerClusterGroup?