Android - How to get those locations from my datab

2019-08-29 23:14发布

问题:

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