2014-12-23 130 views
0

我找到了很多解決方案來計算兩個位置之間的距離,就像 how to find the distance between two geopoints?Calculating distance between two geographic locations 或更具體,這裏的.distanceTo方法的代碼:Android - 如何從我的數據庫中獲取位於我位置附近給定距離內的位置?

Location locationA = new Location("point A"); 

locationA.setLatitude(pointA.getLatitudeE6()/1E6); 
locationA.setLongitude(pointA.getLongitudeE6()/1E6); 

Location locationB = new Location("point B"); 

locationB.setLatitude(pointB.getLatitudeE6()/1E6); 
locationB.setLongitude(pointB.getLongitudeE6()/1E6); 

double distance = locationA.distanceTo(locationB); 

如果我有一個位置(我們說「locationA」),我有一個帶有POI位置數據的數據庫,我只對那些位置感興趣,位於給定距離(比如說「距離」)內的位置「locationA」我如何獲得這些?

如果我寫的方法

private Location[] poiAroundMe (Location locationA, double distance) { 
    // The necessary calculation 
    return locationsAroundMeWithinTheGivenDistance[]; 
} 

什麼是 「必要的計算」?

我很確定,沒有內置的方法,所以我想尋求幫助。

謝謝!

回答

1

在您的數據庫中進行選擇,以獲取包含您的「距離圓」的正方形內的所有位置。

SELECT * 
FROM TblLocations 
WHERE longitude < x 
AND longitude > y 
AND latitude < a 
AND latitude > b; 

將數據放入一個數組中,並將其提供給您的poiAroundMe()方法。 在裏面你可以用你已經知道calcualte法檢查每對距離你locationA並過濾所有,其中有一個小的距離,你locationA並把它們放到你的locationsAroundMeWithinTheGivenDistance []

這不是那樣乾淨你也許希望,但它會工作,我認爲。 ;)

+1

是的,這就是我想到的,而不是「距離圓」,我應該使用「距離平方」。不像我第一次想要的那樣準確,但是,嘿,它工作。 :-)經度 myLongitude - 距離和緯度 myLatitude - 距離; – lpasztor

相關問題