I built a simple map app where I have a layer on top of a map. In Leaflet, how can I check if the layer is already present?
1 Answer
There is a hasLayer
method provided by Leaflet. It's a Boolean
that returns true if already on the map.
For example, map.hasLayer(yourUserDefinedLayerNameGoesHere);
will return true
if it is added to the map or false
if not.
For more details search the Leaflet documentation for hasLayer
at: http://leafletjs.com/reference-1.2.0.html#map-methods-for-layers-and-controls
-
You need to pass through a reference to the Layer object, not the name of the layer. The variable name in this example is ambiguous around this. Just wanted to clear it up for everyone else ;) Oct 13, 2021 at 5:18