mercredi 4 mars 2015

I need to find all addresses that are in a specific distance to the selected point should I retrieve all addresses from DB?

I am developing an application that is supposed to show addresses that are in a specific distance of another location. I know how to find the distance between two points, but the problem is I do not want to retrieve all the addresses and check them one by one toward the selected address. Is there any way to minimize the number of items that I retrieve from database?


Imagine I have 1 million records do I have to retrieve them all and calculate their distance to the selected point?



public class Address{
long Id;
Double latitude;
Double longitude;
..
}


Calculation



public static double distFrom(double lat1, double lng1, double lat2, double lng2) {
double earthRadius = 3958.75;
double dLat = Math.toRadians(lat2-lat1);
double dLng = Math.toRadians(lng2-lng1);
double sindLat = Math.sin(dLat / 2);
double sindLng = Math.sin(dLng / 2);
double a = Math.pow(sindLat, 2) + Math.pow(sindLng, 2)
* Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2));
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double dist = earthRadius * c;

return dist;
}

Aucun commentaire:

Enregistrer un commentaire