我有我当前的位置 lat long,我有一个地方列表,那里有 lat long。
我想做的是弄清楚我是否在其中一个地方附近,附近会是+100m。我不想显示地图,只要知道我是否在附近。
什么样的 php 库可用于比较位置/纬度?或者我可以用数学解决它吗?
谢谢
我有我当前的位置 lat long,我有一个地方列表,那里有 lat long。
我想做的是弄清楚我是否在其中一个地方附近,附近会是+100m。我不想显示地图,只要知道我是否在附近。
什么样的 php 库可用于比较位置/纬度?或者我可以用数学解决它吗?
谢谢
这个问题可以通过使用地球上的球坐标最容易地解决。你以前处理过这些吗?这是从球坐标到普通直角坐标的转换,其中 a=纬度,b=经度,r 是地球的半径:
x = r Cos[a] Cos[b]
y = r Cos[a] Sin[b]
z = r Sin[a]然后我们将使用点积的以下属性(记为 [p,q]):
[p,q] = 长度[p] * 长度[q] * Cos[p & q 之间的角度]
(...)
至少,如果身高对你来说并不重要。如果您需要高度和/或距离取决于道路或可步行性(这甚至是一个词吗?),我认为谷歌地图会更准确。
考虑到它们的球坐标(纬度/经度),计算两点之间的距离并不难。在 Google 上快速搜索“纬度经度距离”会发现这个等式。
显然是这样的:
acos(cos(a.lat) * cos(a.lon) * cos(b.lat) * cos(b.lon) +
cos(a.lat) * sin(a.lon) * cos(b.lat) * sin(b.lon) +
sin(a.lat) * sin(b.lat)) * r
wherea
和b
是您的点,并且r
是地球的平均半径(6371 公里)。
一旦您能够根据坐标计算两点之间的距离,您将需要遍历所有地标并查看您当前的位置是否在一个附近。
但是,如果您有许多地标,您可能需要使用空间搜索算法(可能使用四叉树或类似的数据结构)。
http://blog.wekeroad.com/2007/08/30/linq-and-geocoding用于 LINQ 示例 http://www.codeproject.com/KB/cs/distancebetweenlocations.aspx用于 C# 和 TSQL 示例
我不熟悉这个问题的软件库。但是如果你在 2D 空间中说话,那么我想到了一些数学:
您可以使用以下计算找到 2D 空间中任意 2 个点的距离:
距离 = sqrt( (X2 - X1)^2 + (Y2 - Y1 )^2 )
其中 ^2 表示以 2 为动力。
所以假设你有一个 Point 对象数组(这里我为 Points 定义了一个简单的类),这样你就可以找出哪些点是相邻的:
class Point {
protected $_x = 0;
protected $_y = 0;
public function __construct($x,$y) {
$this->_x = $x;
$this->_y = $y;
}
public function getX() {
return $this->_x;
}
public function getY() {
return $this->_y;
}
public function getDistanceFrom($x,$y) {
$distance = sqrt( pow($x - $this->_x , 2) + pow($y - $this->_y , 2) );
return $distance;
}
public function isCloseTo($point=null,$threshold=10) {
$distance = $this->getDistanceFrom($point->getX(), $point->getY() );
if ( abs($distance) <= $threshold ) return true;
return false;
}
public function addNeighbor($point) {
array_push($this->_neighbors,$point);
return count($this->_neighbors);
}
public function getNeighbors() {
return $this->_neighors;
}
}
$threshold = 100; // the threshold that if 2 points are closer than it, they are called "close" in our application
$pointList = array();
/*
* here you populate your point objects into the $pointList array.
*/
// you have your coordinates, right?
$myPoint = new Point($myXCoordinate, $myYCoordinate);
foreach ($pointList as $point) {
if ($myPoint->isCloseTo($point,$threshold) {
$myPoint->addNeighbor($point);
}
}
$nearbyPointsList = $myPoint->getNeighbors();
编辑:对不起,我忘记了线性距离公式。X 和 Y 轴距离值都应该以 2 为幂,然后它们的总和的 sqrt 就是结果。代码现已更正。