判断经纬度是否在范围内

14

我想确定一个经纬度位置是否在界限内,并寻求算法方面的建议。(使用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
你在问我们这是否可行?我正想问你它是否奏效。 - Scott Saunders
1
如果你的边界包括东经和西经坐标,我认为你会遇到一些问题。根据你使用的坐标系统,这可能涵盖了覆盖南北极地区、0度经线(英格兰、非洲等)和180度W/E(太平洋)的区域。 - netfire
@ScottSaunders 是的,我更多地是在问它是否合乎逻辑。 - Tegan Snyder
@netfire的坐标将在美国。 - Tegan Snyder
5个回答

24

您发帖中的简单比较对于美国坐标是有效的。然而,如果您想要一个能够安全地跨越国际日期变更线(经度为±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;
}

经度有时也会在[0,360]范围内提供。 :/ - naught101

9

你询问了JavaScript和PHP,因为我需要使用PHP,所以我将CheeseWarlock的出色回答转换为PHP。与JavaScript相比,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;
}

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
}

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);
  }

0
这是我的代码,我修改了它以适用于MySQL。现在我们可以直接从SQL查询中检查我们的经纬度是否在范围内。
特别感谢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 ;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接