How to implement a tinder-like search based on distance in Android?

2.5k Views Asked by At

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 !

4

There are 4 best solutions below

4
On

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**
      }
0
On

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

0
On

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

0
On

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.