Determine if point is within bounding box

Do just as usual:

if( bb.ix <= p.x && p.x <= bb.ax && bb.iy <= p.y && p.y <= bb.ay ) {
    // Point is in bounding box
}

bb is the bounding box, (ix,iy) are its top-left coordinates, and (ax,ay) its bottom-right coordinates. p is the point and (x,y) its coordinates.


This solution also takes in consideration a case in which the UI sends a box which crosses longitude 180/-180 (maps views on low zoom level where you can see the whole world, allow infinite cyclic horizontal scrolling, so it is possible for example that a box's bottomLeft.lng=170 while topRight.lng=-170(=190) and by that including a range of 20 degrees.

def inBoundingBox(bl/*bottom left*/: Coordinates, tr/*top right*/: Coordinates, p: Coordinates): Boolean = {
    // in case longitude 180 is inside the box
    val isLongInRange =
      if (tr.long < bl.long) {
        p.long >= bl.long || p.long <= tr.long
      } else
        p.long >= bl.long && p.long <= tr.long

    p.lat >= bl.lat  &&  p.lat <= tr.lat  &&  isLongInRange
}

If you are using leaflet, you can create a new LatLngBounds and use its contains() operation:

var bounds = new L.LatLngBounds(
 new L.LatLng(gc.bbox['_northEast'].lat, gc.bbox['_northEast'].lng),
 new L.LatLng(gc.bbox['_southWest'].lat, gc.bbox['_southWest'].lng));

return bounds.contains(new L.LatLng(pos.latitude, pos.longitude))