2014-12-23 86 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

相关问题