2009-04-28 85 views
1

我已經得到了我目前的地理位置lat,並且我已經有了一個地方列表並且有很長的一段時間。弄清楚我是否在附近東西

我想要做的是弄清楚我是否在附近的一個地方,附近會是+ 100米。我不想顯示地圖,只知道我是否在附近。

什麼樣的php庫可用於比較位置/ lat長?或者我可以用數學解決它?

感謝

+0

您是否在談論少數幾個地理區域或大量距離較遠的地點?幾個點靠近在一起的解決方案不會擴展到地球曲率起作用的距離上的大量數據。 – 2009-04-29 01:03:23

+0

只是在我的地區真正在談論少數幾個地方,而不是大量的地點。 – Tom 2009-05-01 08:28:12

回答

7

Using Longitude and Latitude to Determine Distance

這個問題可以通過使用在 地球球面座標最容易解決。你之前處理過這些嗎?下面是從 球面座標正常直角座標,變換其中a =緯度 且b =經度,並且r是地球的半徑:

X = R的Cos [A]的Cos並[b]
Y = ř的Cos [α]仙並[b]
Z = R仙並[a]

然後,我們將使用點積的以下特性(記譜[p,q]):

[p, q] =長度[p] *長度[q] * Cos [角度q]

(...)

至少,如果高度是不是對你很重要。 如果你需要高度和/或距離依賴道路或步行能力(這是一個單詞?),我認爲谷歌地圖會更精確。

+0

不要高度不重要,也不是可行性:) 感謝您的鏈接,認爲我需要多讀一點點... – Tom 2009-04-28 09:50:38

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或類似的數據結構)。

1

我不熟悉的軟件庫這個問題。但如果你是在二維空間說着話,這裏是一些在我腦海中的數學:

,你可以使用這種計算髮現任何2點在二維空間的距離:

距離=開方(( X2 - X1)^ 2 +(Y2 - Y1)^ 2)

inwhich^2個裝置搭載2.

所以le'ts說你有點對象(這裏的陣列我定義一個簡單的類對於點),通過這種方式你可以找出哪些點是相鄰的:

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就是結果。代碼現在已更正。

0

我對包還不熟悉,但 GeoClass可能是有用的。

我在FreeGIS網站上發現它。如果這不是你想要的,那麼軟件部分列出了許多其他的PHP軟件包。