18

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 1

30

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

1
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.