Teg*_*der 8 javascript php geospatial
我对确定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
}
Run Code Online (Sandbox Code Playgroud)
这会有用吗?谢谢
小智 16
您帖子中的简单比较适用于美国的坐标.但是,如果您想要一个可以安全检查国际日期变更线(经度为±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;
}
Run Code Online (Sandbox Code Playgroud)
当你问起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;
}
Run Code Online (Sandbox Code Playgroud)