I'm trying to load a geoJSON layer with div icons, since they are supposed to be more lightweight.
My problem is that I cannot get the divIcons to display. The geoJSON object is stored as individualPoints
. I've tried styling the icon directly:
var myIcon = L.divIcon({className: 'my-div-icon'});
var geoJsonLayer = L.geoJson(individualPoints, {icon: myIcon});
map.addLayer(geoJsonLayer); //shows standard icon
and styling the icon on each layer:
var geoJsonLayer = L.geoJson(individualPoints, {
onEachFeature: function (feature, layer) {
layer.setIcon(myIcon);
}
});
map.addLayer(geoJsonLayer); // nothing displays
and even creating an additional marker, and adding that to the map:
// 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) {
// make a marker and add to map individually
markersID[feature.properties.id] = L.marker(switchLatLong(feature.geometry.coordinates),{icon: myIcon});
map.addLayer(markersID[feature.properties.id]); //nothing displays on map
}
});
The first code snippet shows the standard leaflet icon; the second and third snippets do not display anything.
Question: How does one load a geoJson using divIcon?
Edit: Using the
new
keyword on the second and third snippets displays the divIcon, but this doesn't work in the first code snippet. Also, I would still like to have the divIcon appear without iterating through the layers to generate a new marker. Also, why must the new keyword be used?
var geoJsonLayer = L.geoJson(individualPoints, {
onEachFeature: function (feature, layer) {
// set icon to a new DivIcon
layer.setIcon(new L.DivIcon());
}
});
map.addLayer(geoJsonLayer); // divIcon displays on map