-2

My application server having thousands of users with their location(LatLng). I want nearest 50 users from any user's location to be displayed in app? How i can fast filter to get the nearest 50 users(LatLng) from server database?

I tried with Sort list of lon\lat points, start with nearest... but taking much time to sort and plot result on map. Please suggest.

Community
  • 1
  • 1
SudP
  • 88
  • 1
  • 7

1 Answers1

0

First lets assume you have all the LatLng points into a list. (received by the database even if they are not sorted or somehow)

Use this function to calculate between LatLng's points.

public double CalculationByDistance(LatLng StartP, LatLng EndP) {

    int Radius = 6371;// radius of earth in Km
    double lat1 = StartP.latitude;
    double lat2 = EndP.latitude;
    double lon1 = StartP.longitude;
    double lon2 = EndP.longitude;
    double dLat = Math.toRadians(lat2 - lat1);
    double dLon = Math.toRadians(lon2 - lon1);
    double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
            + Math.cos(Math.toRadians(lat1))
            * Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2)
            * Math.sin(dLon / 2);
    double c = 2 * Math.asin(Math.sqrt(a));
    double valueResult = Radius * c;
    double km = valueResult / 1;
    DecimalFormat newFormat = new DecimalFormat("####");
    int kmInDec = Integer.valueOf(newFormat.format(km));
    double meter = valueResult % 1000;
    int meterInDec = Integer.valueOf(newFormat.format(meter));

    return Radius * c;

}

StartP should be the user's LatLng.

EndP should be all the other LatLngs

You could create a list inserting all the distances between the user's LatLng and other's LatLng, after that sort that list. Now, you have a sorted list with the distances between the user's LatLng and the rest LatLngs, so with a for loop you could do whatever you want to. If it's taking time you could add a progress bar informing the user that something is happening/loading etc.

Community
  • 1
  • 1
Antonios Tsimourtos
  • 1,676
  • 1
  • 14
  • 37
  • I tried using **distancebetween** for 2 location points, but for huge data this is not feasible solution. So i was trying to filter location points even before calculating distance – SudP Jul 13 '16 at 13:04