2010-10-13 44 views
1

我在我的數據庫中有位置。一個位置有屬性的緯度和經度(從谷歌地圖取得,例如:48.809591)。 是否有任何查詢可以幫助我檢索另一個位置範圍內的位置?確定一定範圍內的經度和緯度

例子: 我有隨緯度= 48.809591和經度= 2.124009的位置,並希望取回5英里位置的範圍內,在我的數據庫中的所有位置畫線A

我首先想到的是檢索在一個廣場的位置location.latitude < A.latitude + 5 miles and location.latitude> A.latitude - 5英里和location.longitude < A.longitude + 5 miles and location.longitude> A.longitude - 5 miles,and然後從類似於http://www.movable-type.co.uk/scripts/latlong.html

的幫助中移除返回陣列中的無關位置?

+0

您正在使用什麼數據庫管理系統? – 2010-10-13 19:28:46

+0

位置恰好在5英里半徑有多重要? (例如,它們位於「角落」還是正方形?) – Randy 2010-10-13 19:31:38

回答

2

就在你使用MySQL作爲數據庫管理系統情況下,你可能有興趣在檢查出下面的介紹:

作者描述瞭如何在MySQL中使用Haversine Formula以通過鄰近度對空間數據進行排序並將結果限制爲定義的半徑。更重要的是,他還介紹瞭如何使用經緯度列上的傳統索引來避免此類查詢的全表掃描。


即使你是不是,這仍然是有趣的和適用的。
演示文稿中還有一個pdf version

0

您將需要一個距離函數。

對於SQL Server它會是這個樣子(注意,距離在千米),

CREATE FUNCTION distance 
    (
     @startLatitude float, 
     @startLongitude float, 
     @endLatitude float, 
     @endLongitude float 
    ) 
    RETURNS float 
    AS 
    BEGIN 

     DECLARE @distance float; 

     set @distance = 
     6371 * 2 * atn2(sqrt(power(sin(pi()/180 * (@endLatitude - @startLatitude)/2), 2) + 
     power(cos(@startLatitude * pi()/180), 2) * 
     power(sin(pi()/180 * (@endLongitude - @startLongitude)/2), 2)), 
     sqrt(1 - power(sin(pi()/180 * (@endLatitude - @startLatitude)/2), 2) + 
     power(cos(@startLatitude * pi()/180), 2) * 
     power(sin(pi()/180 * (@endLongitude - @startLongitude)/2), 2))); 
     RETURN @distance 
    END 
+0

請注意,SQL Server 2008具有內置的地理空間功能,具有原生距離功能和地理空間索引。使用類似上述的UDF需要全表掃描以按距離排序結果。 – 2010-10-13 19:41:21

相關問題