Leaflet JavaScript – Highlight Feature by ID

javascriptleaflet

I have a map application where:

  1. I load polygons as GeoJSON objects
  2. I draw all polygons in a single layer
  3. I highlight a polygon on mouse hover

What I need to do is the following: on mouse hover, I need to highlight multiple polygons (I get the list of polygons somehow – irrelevant -). I have the IDs of all the polygons I need to highlight, but I can't see how can I access them though the API.

Best Answer

You would have to store polygon IDs in onEachFeature(feature, layer) handler (e.g. layer._polygonId = feature.id), so later you can iterate over layers (e.g. jsonLayer.eachLayer(function(layer) { setHighlighted(layer, doesRelate(layer._polygonId, selectedId)); });.

Related Question