I am developing an Android application. I need to find the distance between two geo coordinates.
I used Location.distanceBetween()
and Location.distanceTo()
functions. These functions give the straight distance, but the actual distance is different when we travel by road.
Using the JavaScript equivalent of the Haversine formula, we can determine the length of the Polyline, the straight distance between our two markers. The function accepts two marker objects and returns the distance between them in miles.
The first tap point in the map will be the source of the route and the second tap point in the map will be the destination of the route. On taping the second point, a driving route will be drawn in the Google Map Android API V2 using Google Directions API.
This example demonstrates how do I get the distance between two geographic locations in android. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml.
This service is also available as part of the client-side Maps JavaScript API , or for server-side use with the Java Client, Python Client, Go Client and Node.js Client for Google Maps Services. The Distance Matrix API is a service that provides travel distance and time for a matrix of origins and destinations.
When the straight line distance is not adequate, you’ll need more than a formula to determine the travel distance. Driving directions are one of the most popular features in Google Maps, so it’s unsurprising that it’s also made available via an API.
Use google api as
public float getDistance(double lat1, double lon1, double lat2, double lon2) {
String result_in_kms = "";
String url = "http://maps.google.com/maps/api/directions/xml?origin=" + lat1 + "," + lon1 + "&destination=" + lat2 + "," + lon2 + "&sensor=false&units=metric";
String tag[] = {"text"};
HttpResponse response = null;
try {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(url);
response = httpClient.execute(httpPost, localContext);
InputStream is = response.getEntity().getContent();
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(is);
if (doc != null) {
NodeList nl;
ArrayList args = new ArrayList();
for (String s : tag) {
nl = doc.getElementsByTagName(s);
if (nl.getLength() > 0) {
Node node = nl.item(nl.getLength() - 1);
args.add(node.getTextContent());
} else {
args.add(" - ");
}
}
result_in_kms =String.valueOf( args.get(0));
}
} catch (Exception e) {
e.printStackTrace();
}
Float f=Float.valueOf(result_in_kms);
return f*1000;
}
Or you can use following function
public final static double AVERAGE_RADIUS_OF_EARTH = 6371;
public float calculateDistance(double userLat, double userLng, double venueLat, double venueLng) {
double latDistance = Math.toRadians(userLat - venueLat);
double lngDistance = Math.toRadians(userLng - venueLng);
double a = (Math.sin(latDistance / 2) * Math.sin(latDistance / 2)) +
(Math.cos(Math.toRadians(userLat))) *
(Math.cos(Math.toRadians(venueLat))) *
(Math.sin(lngDistance / 2)) *
(Math.sin(lngDistance / 2));
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return (float) (Math.round(AVERAGE_RADIUS_OF_EARTH * c));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With