4

I have started to develop an Android application where I need a distance based search. The user should be able to choose the maximum distance, let's say 3 km. I have already written some code where I get every user's current location (Lat, Lng) and store it in a mysql database. Then the app retrieves every user's position and finds the distance between the user and the other ones. With an if loop, the app checks if the value entered by the user is less or equal to each user distance.

I have tested it and it's working well, but the problem is that I wonder if this would still work with thousands of users ?

If you have any advice or answer to this, please tell me !

David Seroussi
  • 1,650
  • 2
  • 17
  • 34
  • you can look for the [haversine formula](https://en.wikipedia.org/wiki/Haversine_formula) to calculate the actual distance between two points. Then check if `result =< MAX_DIST` in order to add it. – KarelG Feb 13 '16 at 09:51
  • hii @David did you find any good solution for this, i like your question i want something like this.. can you explain your question in detail and also with some code!? – Rucha Bhatt Joshi Aug 17 '17 at 13:32
  • i wrote one in NODE if your interested https://github.com/designalchemy/node-tinder-sort-by-distance/blob/master/tinder.js – Luke Robertson Feb 18 '19 at 08:23

4 Answers4

0

Let the current user location be assigned to currentLocation.

      Location databaseLocation = new Location("databaseLocation");
      databaseLocation.setLatitude(database_latitude);
      databaseLocation.setLongitude(database_longitude);
      //< 3000 meters = 3km
      if (currentLocation.distanceTo(databaseLocation) < 3000) {
        //**This entry is within limits**
      }
Vaibhav Sharma
  • 2,293
  • 1
  • 17
  • 24
  • Thanks, but I've already done something like that. I want to know if this is still going to work with a lot of users, like 50 000. Because if the app has to calculate 50 000 distances,then how long is this going to take .. – David Seroussi Feb 13 '16 at 12:42
  • For this you can make lets say 5 asynchronous calls at a time. Till you get all the results lets say in a list, you can show a loader. But the best approach will be to send your coordinates to backend which will give results quickly.This won't take much time. – Vaibhav Sharma Feb 13 '16 at 13:48
  • Ohhh... I just re-read your question and realised all your locations are in a database. For such a case this won't be taking much time. Go ahead and implement it (y) – Vaibhav Sharma Feb 13 '16 at 13:50
  • @DavidSeroussi how you do that? **I get every user's current location (Lat, Lng)** how you get all users lat long? – Rucha Bhatt Joshi Aug 17 '17 at 13:36
0

Here is the complete method for finding distance between two locations just give two lats longs as parameters and it will return the distance in string.

public static String distFrom(double current_lat, double current_long, double latst, double longst) {
    double earthRadius = 3958.75;
    double dLat = Math.toRadians(latst-current_lat);
    double dLng = Math.toRadians(longst-current_long);
    double a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(Math.toRadians(lat)) * Math.cos(Math.toRadians(latst)) * Math.sin(dLng/2) * Math.sin(dLng/2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    double dist = earthRadius * c;
    double meterConversion =  1.609344;       //Kilometer
  //for two digits float value:
    String s = String.format("%.2f", (dist * meterConversion));

    return s;

    }

The return distance is in kilometers you can further covert it into miles

Dayvon
  • 87
  • 2
  • 11
0

I wonder if you really need to calculate distance for each user. Will it be helpful if you filter the users that are possible to be in that radius of 3 km by may be having an upper and lower limits on the latitude and longitudes. From what I have seen usually a change on the second decimal point results in difference of a km. first location: lat: 13.756331 long: 100.501762

lat: 13.746331 long: 100.501762 => different with first location is 1km lat: 13.756331 long: 100.511762 => different with first location is 1km

I haven't thought too much about it, this is just a thought I would love get some input on this

hiten pannu
  • 186
  • 1
  • 6
0

I don't know how this will implemented on the backend but I can recommend looking up spatial objects and R-trees.

An object is characterized as spatial if it has at least one attribute that captures its location in a 2D or 3D space. Moreover a spatial object is likely to have geometric extent in space. For example, we can say that a building is a spatial object, since it has a location and a geometric extent in a 2D or 3D map.

The “R” in R-tree stands for rectangle. The key idea of the data structure is to group nearby objects and represent them with a rectangle. The minimum bounding rectangle or MBR for short. This happens recursively.

Since all objects lie within this bounding rectangle, a query that does not intersect the bounding rectangle also cannot intersect any of the contained objects. At the leaf level, each rectangle describes a single spatial object. All the other levels are simply node pointers.

You can use this to filter fast users, because you are going to use these trees, that are nearby according to the MBR of the user and where other MBRs intersect.

leo_bouts
  • 329
  • 2
  • 17