0

I'm working on a Android app which have feature of nearby friends. So each user have list of friends stored in a Firebase real time database. Also their locations are saved there while they are moving around.

What I want to do?

I want to compare all the user freinds locations with the user location. And if the distance between the user and his friend is one km or less then the nearby friends list will be updated. This check will be done for reach friend for the user that is using the app.

What I tried to?

Before posting this question I searched around and found one answer on Stack Overflow. But I don't get the function which calculates the distance because the variables names are short and not self explanatory.

Based on the written above does anyone knew how can I calculate distance between two location or can summarize what the functions does which is shown on the link above.

Jovan
  • 306
  • 6
  • 20

2 Answers2

1

You can use distanceBetween() method from the Location class.

gsb
  • 5,520
  • 8
  • 49
  • 76
1

firstLocation.distanceTo(secondLocation)

you will get distance from second location to first one in meters.

Yasir Ayaz
  • 91
  • 1
  • 5
  • They are LatLng type which doesn't allow me to user distanceTo. – Jovan Mar 05 '18 at 18:39
  • You can create a location object from lat long like this Location firstLocation = new Location(""); firstLocation.setLatitude(yourLat);//your coords of course firstLocation.setLongitude(yourLon); – Yasir Ayaz Mar 05 '18 at 18:44
  • Yeah that is what I did and then compared them and I worked. The only problem is that the distance is compared by air and not by distance on ground. – Jovan Mar 05 '18 at 18:47
  • @jovan Getting ground distance would require a mapping library, and probably cost a good amount (and will definitely take a lot of time). That's why when you do searches almost everything uses direct distance. – Gabe Sechan Mar 05 '18 at 18:51
  • Oh, then this will do the work for now. Thanks a lot. – Jovan Mar 05 '18 at 19:16