I have added some GeoJSON to my Leaflet powered map as below. (Altered version)
var array_of_layers = L.layerGroup([]);
var styleholder = [];
var belgium_style = {color: "#d3e46f"};
var bosnia_style = {color: "#f3c1d3"};
var (...)
function styling(name, geoname, color){
this.numid = count;
styleholder[count] = this;
this.name = name;
this.geoname = geoname; //the geojson filename, just in case.
this.label = name;
this.color = color;
this.opacity = 1;
this.stroke = false;
this.fillOpacity = 1;
this.fillColor = color;
count++;
}
var belgium_style = new styling("Belgium", "belgium", "#52EB8D");
var bosnia_style = new styling("Bosnia", "bosnia", "#EB9752");
(...)
var everything = L.geoJson(myfile, {
onEachFeature: function(feature){
array_of_layers.addLayer(feature);
},
style: function(feature){
switch(feature.properties.name){
case "belgium": return belgium_style; break;
case "bosnia": return bosnia_style; break;
case "denmark": return denmark_style; break;
case "great_britain": return britain_style; break;
case "greece": return greece_style; break;
case "italy": return italy_style; break;
case "serbia": return serbia_style; break;
case "spain": return spain_style; break;
}
}
});
So the next thing I need to do is, for instance, make a country become blue and all the others gray, when I mouse over it. And, make that possible as well by a function, that I can call whenever I want, regardless of mouse position.
Therefore, during runtime, how do I change the color of any country as I wish? In other words, what is the correct syntax for re-defining the style function for a particular feature?
I have tried to iterate over the styleholder
array with a for(var x in styleholder)
loop until I get to find the style with the name I want to make blue. By looking at the debugging console, I can tell that I get to find the country, but there's no use changing the color value of a country once it's been created on the map. So the color doesn't change.
The code below sums up what I'm talking about:
var change_color_to_blue = function(country){
for(var a in styleholder){
if (a.geoname === country){
a.color = "#0000FF";
a.fillColor = "#0000FF";
}
}
change_color_to_blue("belgium");
You see, although it runs without an error, the color remains the same.
The same thing for a mousing-over feature, could be possibly something like this:
everything.on('mouseover', function(e){
var moused_over_country = e.layer;
moused_over_country.color = "#0000FF"; //this is not how it's done
MagicallyMakeAllOtherLayersGray();
refreshgeoJson(); //that does not exist.
refreshMap(); //this neither, most probably.
});
Even this works, and the target country gets blue, how do I make the rest gray?
Is there a way to iterate over every feature in geoJson and reset every style function respectively?
Best Answer
I have written down a small code to modify geojson feature style on runtime using leaflet. you can try it on jsFiddle or use the following code test locally.
I hope it will help.
Here is the code: