I'm building a leaflet map using GeoJSON data. I run into problems when trying to set the icon I use based on a GeoJSON property. I think my error is related to calling an object with a string, but I can't figure out what it is exactly.
Here's my code:
Each item in the GeoJSON has an iconcategory property as below :
{"type":"Feature",
"properties":{
"iconcategory": "iconGreyHouse",
...
For each icon category there's an icon variable as follows :
var iconGreyHouse = L.icon({
iconUrl: "/markerIcons/house_icon_grey.png",
iconSize: [20, 20]
});
Finally, when I load my GeoJSON file, I get "iconcategory" from my GeoJSON properties in the hopes of picking the corresponding icon variable...
$.getJSON("/GeoJSON/housemarkers.geojson", function (houses) {
L.geoJson(houses, {
pointToLayer: function (feature, latlng) {
return L.marker(latlng, {
icon: feature.properties.iconcategory
});
}
}).addTo(housemarkers);
});
This is where it doesn't work! If I use the exact same code but specify an icon variable name directly, all works fine; it's only when I try to set the icon via the feature.property that it fails.