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?
Asked
Active
Viewed 4.3k times
1 Answers
33
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
Stephen Lead
- 21,119
- 17
- 113
- 240
Vic
- 346
- 3
- 2
-
3You 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 ;) – Chris Schaller Oct 13 '21 at 05:18