4

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:

Steve
  • 25,806
  • 2
  • 33
  • 43
colbin8r
  • 342
  • 2
  • 12
  • 1
    I think some leaflet plugins like markerclusterer do optimizations to hide elements that are not visible in the current bounds. Maybe you could look there for ideas. – Azmisov Dec 21 '17 at 00:52
  • I'm looking for something fairly similar, and I've got turf.js in my back pocket. If you turn the bound into a rectangle polygon, you can probably check if they have any ovelaps – JonRed May 02 '18 at 00:54

0 Answers0