确定纬度/经度 [英] Determine if Lat/Lng in Bounds
本文介绍了确定纬度/经度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对确定经/纬度位置是否在范围内以及寻找有关算法的建议感兴趣. (javascript或php)
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
}
这行得通吗?谢谢
推荐答案
帖子中的简单比较适用于美国的坐标.但是,如果您想要一种可以安全检查国际日期范围(经度为±180°)的解决方案:
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;
}
这篇关于确定纬度/经度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文