I'm building a Leaflet plugin that adds/removes layers to/from a Leaflet map. My plugin needs to know if a given layer is visible on-screen. So far, I've come up with the following criteria that a layer must meet in order to be considered "visible":
- It must be added to the map. (This is trivial to check in my case.)
- It must be within a valid zoom range. (This is also pretty easy to handle.)
- It must be visible within the current extent/bounds.
The third criteria is the one I am having difficulty checking. My layers are a variety of different Esri-Leaflet layers. I understand that I can check whether a given point or rectangle is contained within the visible bounds of the map with map.getBounds().contains(...)
, but my problem is I am not sure how to represent my Esri-Leaflet layer as a rectangle. Not even all Leaflet layers have a getLatLng()
method, so this question isn't even Esri-Leaflet specific (e.g. how would you perform the same check on a TileLayer)?
How can I get the bounds of a Leaflet layer, or otherwise check if the layer is visible on the map?
Not a duplicate of: