Get the distance between two geo points


Question

I want to make an app which checks the nearest place where a user is. I can easily get the location of the user and I have already a list of places with latitude and longitude.

What would be the best way to know the nearest place of the list against the current user location.

I could not find anything in the google APIs.

1
106
12/27/2018 12:39:31 PM

Accepted Answer

Location loc1 = new Location("");
loc1.setLatitude(lat1);
loc1.setLongitude(lon1);

Location loc2 = new Location("");
loc2.setLatitude(lat2);
loc2.setLongitude(lon2);

float distanceInMeters = loc1.distanceTo(loc2);

Reference: http://developer.android.com/reference/android/location/Location.html#distanceTo(android.location.Location)

151
12/15/2013 10:12:33 PM

http://developer.android.com/reference/android/location/Location.html

Look into distanceTo or distanceBetween. You can create a Location object from a latitude and longitude:

Location location = new Location("");
location.setLatitude(lat);
location.setLongitude(lon);

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon