I need to find the estimate drive time from one place to another. I've got latitudes and longitudes for both places but I have no idea how to do that. Is there is any API for that. help thanks.

- 27,569
- 23
- 102
- 149

- 1,365
- 2
- 15
- 31
-
why not use google maps? – Jeff Dec 07 '11 at 05:27
-
I just need estimated time not directions... – ZeeShaN AbbAs Dec 07 '11 at 07:39
5 Answers
yes you get the time and distance value as well as many like direction details in driving, walking etc mode. all you got from the google direction api service
check our this links

- 30,639
- 18
- 84
- 159
-
thanks for ur reply. but that is for javascript I need for android phone application.. – ZeeShaN AbbAs Dec 07 '11 at 05:58
-
1you can use this service in android also just you need to call the service using http package – Pratik Dec 07 '11 at 06:07
-
I can't find estimated time there. can u provide any reference specific for time. – ZeeShaN AbbAs Dec 07 '11 at 07:45
-
1try to execute this http://maps.googleapis.com/maps/api/directions/json?origin=porbandar&destination=rajkot&sensor=false in browser and save the json file in that you find the duration. From individual step duration as well as total duration travelling available in this – Pratik Dec 07 '11 at 08:08
Location location1 = new Location("");
location1.setLatitude(lat);
location1.setLongitude(long);
Location location2 = new Location("");
location2.setLatitude(lat);
location2.setLongitude(long);
float distanceInMeters = location1.distanceTo(location2);
EDIT :
//For example spead is 10 meters per minute.
int speedIs10MetersPerMinute = 10;
float estimatedDriveTimeInMinutes = distanceInMeters / speedIs10MetersPerMinute;
Please also see this, if above not works for you:

- 1
- 1

- 27,569
- 23
- 102
- 149
-
-
This will return distance in meters & thats why i wrote "Now you have the distanceInMeters so you can calculate the estimated drive time." – Yaqub Ahmad Dec 07 '11 at 05:42
-
-
I don't need the url for navigate between two places. I need the estimated travel time in local variable... – ZeeShaN AbbAs Dec 07 '11 at 05:55
-
I appreciate ur answer but I have got one problem is that I dont know the meeterPerMinutes time and this method doesn't provide the distance calculation road by road. So I think I have to use Google MAP API for that... – ZeeShaN AbbAs Dec 07 '11 at 06:08
-
But doesn't the above code give you the straight-line distance rather than the driving distance?? I don't see how you can simply translate on to the other without mapping/road info. – ban-geoengineering Jun 29 '15 at 16:58
-
2be aware the travel time given by this answer ignores the physical route itself and traffic time – Ryhan Aug 27 '16 at 18:19
Deprecation note The following described solution is based on Google's Java Client for Google Maps Services which is not intended to be used in an Android App due to the potential for loss of API keys (as noted by PK Gupta in the comments). Hence, I would no longer recommened it to use for production purposes.
As already described by Praktik, you can use Google's directions API to estimate the time needed to get from one place to another taking directions and traffic into account. But you don't have to use the web API and build your own wrapper, instead use the Java implementation provided by Google itself, which is available through the Maven/gradle repository.
Add the google-maps-services to your app's build.gradle:
dependencies { compile 'com.google.maps:google-maps-services:0.2.5' }
Perform the request and extract the duration:
// - Put your api key (https://developers.google.com/maps/documentation/directions/get-api-key) here: private static final String API_KEY = "AZ.." /** Use Google's directions api to calculate the estimated time needed to drive from origin to destination by car. @param origin The address/coordinates of the origin (see {@link DirectionsApiRequest#origin(String)} for more information on how to format the input) @param destination The address/coordinates of the destination (see {@link DirectionsApiRequest#destination(String)} for more information on how to format the input) @return The estimated time needed to travel human-friendly formatted */ public String getDurationForRoute(String origin, String destination) // - We need a context to access the API GeoApiContext geoApiContext = new GeoApiContext.Builder() .apiKey(apiKey) .build(); // - Perform the actual request DirectionsResult directionsResult = DirectionsApi.newRequest(geoApiContext) .mode(TravelMode.DRIVING) .origin(origin) .destination(destination) .await(); // - Parse the result DirectionsRoute route = directionsResult.routes[0]; DirectionsLeg leg = route.legs[0]; Duration duration = leg.duration; return duration.humanReadable; }
For simplicity, this code does not handle exceptions, error cases (e.g. no route found -> routes.length == 0), nor does it bother with more than one route or leg. Origin and destination could also be set directly as LatLng
instances (see DirectionsApiRequest#origin(LatLng)
and DirectionsApiRequest#destination(LatLng)
.
Further reading: android.jlelse.eu - Google Maps Directions API

- 5,402
- 47
- 53
-
1Use of this java API is discouraged for client side usage. "The Java Client for Google Maps Services is designed for use in server applications. This library is not intended for use inside of an Android app, due to the potential for loss of API keys." https://github.com/googlemaps/google-maps-services-java – PK Gupta Feb 03 '19 at 17:36
-
It's my best solution with `directionsResult.routes[0].legs[0].duration` getting the **shortest distance by road**. You need to enable DirectionsApi in Google cloud though – Brian May 08 '21 at 09:39
You can also use http://maps.google.com/maps?saddr={start_address}&daddr={destination_address}
it will give in direction detail along with distance and time in between two locations
http://maps.google.com/maps?saddr=79.7189,72.3414&daddr=66.45,74.6333&ie=UTF8&0&om=0&output=kml

- 674
- 9
- 20
-
thanks for your reply but I don't need the url for navigate between two places. I need the estimated travel time in local variable... – ZeeShaN AbbAs Dec 07 '11 at 06:01
Calculate Distance:-
float distance;
Location locationA=new Location("A");
locationA.setLatitude(lat);
locationA.setLongitude(lng);
Location locationB = new Location("B");
locationB.setLatitude(lat);
locationB.setLongitude(lng);
distance = locationA.distanceTo(locationB)/1000;
LatLng From = new LatLng(lat,lng);
LatLng To = new LatLng(lat,lng);
Calculate Time:-
int speedIs1KmMinute = 100;
float estimatedDriveTimeInMinutes = distance / speedIs1KmMinute;
Toast.makeText(this,String.valueOf(distance+
"Km"),Toast.LENGTH_SHORT).show();
Toast.makeText(this,String.valueOf(estimatedDriveTimeInMinutes+" Time"),Toast.LENGTH_SHORT).show();

- 307
- 2
- 6
-
This doesn't take roads into account. For example, it would tell me I could drive from California to Hawaii. – Feathercrown Mar 28 '17 at 12:41