Android - How to get those locations from my database what are within a given distance around my location?

白昼怎懂夜的黑 提交于 2019-12-25 04:12:15

问题


I found a lot of solutions to calculate the distance between two locations, just like how to find the distance between two geopoints? or Calculating distance between two geographic locations or to be more specific, here's a code for the .distanceTo method:

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);

If I have a location (let's say "locationA"), and I have a database with POI-location data, and I'm interested in only those, what are around that location "locationA" within a given distance (let's say "distance"), how do I get those?

If I write a method

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

what would be "the necessary calculation"?

I'm pretty sure, that there's no built-in method for that, so I would like to ask for help.

Thank you!


回答1:


Do a select on your database, to get all the locations which are inside of a square that includes exactly your "distance-circle".

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

Put the data in a array, and give it to your poiAroundMe() -Method. Inside you can check each for the distance to your locationA with a calcualte-method you already know and filter all, which have a smaller distance to your locationA and put them into your locationsAroundMeWithinTheGivenDistance[]

It's not as clean as you perhaps wish, but it will work i think. ;)



来源:https://stackoverflow.com/questions/27616498/android-how-to-get-those-locations-from-my-database-what-are-within-a-given-di

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!