Search code examples
angularlocationleafletcoordinates

Checking if marker coordinates are in bounds


I'm using leaflet map in Angular 6 application. I get location info for each item as

[lat: xx, lng: xx]

I also get the bounds object which describes what area of map is currently visible. It changes with dragging and zooming. The bounds object looks like this:

_northEast
    lat: 76.16399261609192
    lng: 111.00585937500001
_southWest
    lat: 18.646245142670608
    lng: -43.76953125

I know I could just compare each item lat whether it's bigger than southWest.lat and smaller than northEast.lat and same with lng, but is there any easier and more elegant way to fit it in the bounds?

And yes, I looked through the leaflet docs before asking this :)


Solution

  • You need LatLngBounds.contains(LatLng)

    https://leafletjs.com/reference.html#latlngbounds-contains