Tegan Snyder
Tegan Snyder

Reputation: 761

Determine if Lat/Lng in Bounds

I'm interested in determining if a lat/lng location is within the bounds and looking for recommendations on a algorithm. (javascript or php)

Here is what I have so far:

var lat = somelat;
var lng = somelng;

if (bounds.southWest.lat < lat && lat < bounds.northEast.lat && bounds.southWest.lng < lng && lng < bounds.northEast.lng) {
     'lat and lng in bounds
}

will this work? thanks

Upvotes: 14

Views: 7774

Answers (6)

Ricardo Araque
Ricardo Araque

Reputation: 81

Based on CheeseWarlock's solution, but working for Flutter 3:

  pointLatLngIsinBounds(LatLng point, LatLngBounds bounds) {

    var eastBound = point.longitude <    bounds.northEast.longitude;
    var westBound = point.longitude > bounds.southWest.longitude;
    var inLong;

    if (bounds.northEast.longitude < bounds.southWest.longitude) {
      inLong = eastBound || westBound;
    } else {
      inLong = eastBound && westBound;
    }

    var inLat = point.latitude > bounds.southWest.latitude && point.latitude < bounds.northEast.latitude;
    return inLat && inLong;
  }

Upvotes: 0

Raja Peela
Raja Peela

Reputation: 1386

I have tried with below method works for me.

    bool inBounds(LatLng point, LatLngBounds bounds) {
    var x1 = math.min(bounds.southwest.latitude, bounds.northeast.latitude);
    var x2 = math.max(bounds.southwest.latitude, bounds.northeast.latitude);
    var y1 = math.min(bounds.southwest.longitude, bounds.northeast.longitude);
    var y2 = math.max(bounds.southwest.longitude, bounds.northeast.longitude);
    return (x1 < point.latitude &&
        x2 > point.latitude &&
        y1 < point.longitude &&
        y2 > point.longitude);
  }

Upvotes: 0

Gibolt
Gibolt

Reputation: 47097

This is a slightly optimized version of CheeseWarlock's Js answer, that short circuits.

const inBounds = (point, bounds) => {
    const inLat = point.lat > bounds.sw.lat && point.lat < bounds.ne.lat
    if (!inLat) return false

    const eastBound = point.lng < bounds.ne.lng
    const westBound = point.lng > bounds.sw.lng
    return (bounds.ne.lng < bounds.sw.lng)
        ? eastBound || westBound
        : eastBound && westBound
}

Upvotes: 2

Darkcoder
Darkcoder

Reputation: 858

here is mine code i modified it for mysql its mysql function now we can directly check that our long lat is in bounds or not from sql query .

and special thanks to CheeseWarlock and Chris Rae for the solution thanks sir

select inBounds("58.25173","-100.21041","89.71691425364952","180","-88.9294031317244","-180")

and you can call this function in query like this way

DELIMITER $$
CREATE FUNCTION inBounds(pointLat FLOAT, pointLong FLOAT, boundsNElat FLOAT, boundsNElong FLOAT, boundsSWlat FLOAT, boundsSWlong FLOAT) RETURNS VARCHAR(30)
 BEGIN
  DECLARE westBound FLOAT ;
  DECLARE eastBound FLOAT ;
  DECLARE inLong FLOAT DEFAULT 0 ;
  DECLARE inLat FLOAT DEFAULT 0 ;
  IF (pointLong < boundsNElong) THEN 
    SET eastBound = 1;
  End if;
  IF (pointLong > boundsSWlong) THEN
    SET westBound = 1;
  End if;

  IF (boundsNElong < boundsSWlong) THEN
      IF (eastBound || westBound) THEN
         SET inLong = 1;
     END if;
  ELSE 
     IF (eastBound && westBound) THEN 
         SET inLong = 1;
     END IF;
  END IF;
  IF (pointLat > boundsSWlat && pointLat < boundsNElat) THEN
    SET inLat = 1;
  END IF;
  RETURN inLat && inLong;
END$$
DELIMITER ;

Upvotes: 0

Chris Rae
Chris Rae

Reputation: 5665

As you asked about both Javascript and PHP (and I needed it in PHP), I converted CheeseWarlock's great answer into PHP. Which, as usual with PHP, is a lot less elegant. :)

function inBounds($pointLat, $pointLong, $boundsNElat, $boundsNElong, $boundsSWlat, $boundsSWlong) {
    $eastBound = $pointLong < $boundsNElong;
    $westBound = $pointLong > $boundsSWlong;

    if ($boundsNElong < $boundsSWlong) {
        $inLong = $eastBound || $westBound;
    } else {
        $inLong = $eastBound && $westBound;
    }

    $inLat = $pointLat > $boundsSWlat && $pointLat < $boundsNElat;
    return $inLat && $inLong;
}

Upvotes: 9

CheeseWarlock
CheeseWarlock

Reputation: 1371

The simple comparison in your post will work for coordinates in the US. However, if you want a solution that's safe for checking across the International Date Line (where longitude is ±180°):

function inBounds(point, bounds) {
    var eastBound = point.long < bounds.NE.long;
    var westBound = point.long > bounds.SW.long;
    var inLong;

    if (bounds.NE.long < bounds.SW.long) {
        inLong = eastBound || westBound;
    } else {
        inLong = eastBound && westBound;
    }

    var inLat = point.lat > bounds.SW.lat && point.lat < bounds.NE.lat;
    return inLat && inLong;
}

Upvotes: 25

Related Questions