Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

leaflet: Detect when marker is out of view

I have a table with geo location results in which the user can click to set that location in the map. The map is already loaded, and the user might have paned/scolled, and all those locations might be out of sight.

So when the user clicks the button for a location in the table, I need to check if that marker is already visible in the map. And only if invisible, call to setView and zoom into that location; otherwise, I just change the color of the marker (no pan, no zoom).

Has leaflet any API to check if a layer/marker is currenlty been rendered in the map?

like image 829
Mariano Desanze Avatar asked Feb 26 '16 15:02

Mariano Desanze


1 Answers

Probably map.getBounds().contains(myMarker.getLatLng())

References:

  • map.getBounds() Returns the LatLngBounds of the current map view.
  • myLatLngBounds.contains() Returns true if the rectangle contains the given point.
  • marker.getLatLng() Returns the current geographical position of the marker.
like image 161
ghybs Avatar answered Sep 22 '22 23:09

ghybs