12

我有兴趣确定 lat/lng 位置是否在范围内,并寻找有关算法的建议。(javascript或php)

这是我到目前为止所拥有的:

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
}

这行得通吗?谢谢

4

5 回答 5

22

您帖子中的简单比较适用于美国的坐标。但是,如果您想要一个安全地检查国际日期变更线(经度为 ±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;
}
于 2012-06-07T21:24:38.757 回答
8

当您询问 Javascript 和 PHP(我在 PHP 中需要它)时,我将 CheeseWarlock 的出色答案转换为 PHP。与 PHP 一样,它的优雅程度要低得多。:)

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;
}
于 2015-04-02T23:43:40.683 回答
2

这是 CheeseWarlock 的 Js 答案的略微优化版本,即短路。

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
}
于 2020-09-20T07:39:00.923 回答
0

这是我的代码,我为 mysql 修改了它的 mysql 函数,现在我们可以直接从 sql 查询中检查我们的 long lat 是否在界限内。

特别感谢CheeseWarlockChris Rae的解决方案谢谢先生

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

你可以像这样在查询中调用这个函数

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 ;
于 2019-06-04T11:53:41.280 回答
0

我尝试过以下方法对我有用。

    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);
  }
于 2021-10-26T14:00:36.430 回答