Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How to get estimated drive time from one place to another?

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.

like image 698
ZeeShaN AbbAs Avatar asked Dec 07 '11 05:12

ZeeShaN AbbAs


1 Answers

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.

  1. Add the google-maps-services to your app's build.gradle:

    dependencies {
        compile 'com.google.maps:google-maps-services:0.2.5'
    }
    
  2. 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

like image 180
Murmel Avatar answered Oct 01 '22 04:10

Murmel