Kalashir
Kalashir

Reputation: 1127

Check if marker is in view (map) - mapbox

enter image description hereI want to check if marker is on mapview or out of the map. I am putting marker of the map and not able to check if it on currentView or not. I have tried below code. this.map.getBounds().contains(e.layer.getLatLng()); but it is returning me true. map is returning its old lat lng i.e bounds (I think so)

Upvotes: 8

Views: 14819

Answers (2)

Denis
Denis

Reputation: 1268

I use custom function:

function inBounds(point, bounds) {
  var lng = (point.lng - bounds._ne.lng) * (point.lng - bounds._sw.lng) < 0;
  var lat = (point.lat - bounds._ne.lat) * (point.lat - bounds._sw.lat) < 0;
  return lng && lat;
}

Upvotes: 8

ghybs
ghybs

Reputation: 53290

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

See also: area estimation in viewpoint of map using leaflet

Upvotes: 25

Related Questions