【问题标题】:Figure out if I'm nearby something弄清楚我是否在附近
【发布时间】:2009-04-28 09:21:33
【问题描述】:

我有我当前的位置 lat long,我有一个地点列表,那里有 lat long。

我想做的是弄清楚我是否在其中一个地方附近,附近会是 +100m。我不想显示地图,只要知道我是否在附近即可。

有哪些 php 库可用于比较位置/纬度?或者我可以用数学解决它吗?

谢谢

【问题讨论】:

  • 您是指小地理区域中的少数几个位置,还是分布在很远距离上的大量位置?靠近的几个点的解决方案不会扩展到分布在地球曲率起作用的距离上的大量数据。
  • 仅谈论我所在地区的少数几个地方,而不是大量位置。

标签: php geolocation gps gis location


【解决方案1】:

Using Longitude and Latitude to Determine Distance

这个问题可以通过在 地球。你以前处理过这些吗?这是从 球坐标到正常的直角坐标,其中 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 之间的角度]

(...)

至少,如果身高对你不重要的话。 如果您需要高度和/或距离取决于道路或可步行性(这甚至是一个词吗?),我认为谷歌地图会更准确。

【讨论】:

  • 不,身高并不重要,步行性也不重要 :) 感谢您的链接,我想我需要多读一点...
【解决方案2】:

考虑到两点的球坐标(纬度/经度),计算两点之间的距离并不难。在 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

其中ab 是您的点,r 是地球的平均半径(6371 公里)。

一旦您能够根据坐标计算出两点之间的距离,您就需要遍历所有地标,看看您当前的位置是否在一个附近。

但是,如果您有许多地标,您可能希望使用空间搜索算法(可能使用Quadtree 或类似的数据结构)。

【讨论】:

    【解决方案3】:
    【解决方案4】:

    我不熟悉解决这个问题的软件库。但如果你在 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 就是结果。代码现已更正。

    【讨论】:

    • hm,如果你在计算地球上的距离,勾股定理并不真正有效,因为地球大致类似于一个扁球体。考虑一个具有 3 个 90° 角的三角形 - 可能,在曲面上。尽管您可能会在很短的距离内忽略这一点。
    • 这是正确的。我认为当问题是找到附近的距离时,可以忽略曲面和线性板曲面之间的差异。它带有一些近似值。
    【解决方案5】:

    我不熟悉这个包,但是 GeoClass 可能有用。

    我在FreeGIS 网站上找到了它。如果这不是您想要的,软件部分中还列出了许多其他 php 包。

    【讨论】:

      猜你喜欢
      • 2015-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-15
      • 1970-01-01
      • 1970-01-01
      • 2021-06-04
      相关资源
      最近更新 更多