ZeeShaN AbbAs
ZeeShaN AbbAs

Reputation: 1365

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.

Upvotes: 11

Views: 35981

Answers (5)

Murmel
Murmel

Reputation: 5702

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

Upvotes: 2

Pranav Moyal
Pranav Moyal

Reputation: 317

   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();

Upvotes: -2

Yaqub Ahmad
Yaqub Ahmad

Reputation: 27659

    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:

Calculate distance between two points in google maps V3

Upvotes: 11

voidRy
voidRy

Reputation: 684

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

Upvotes: 1

Pratik
Pratik

Reputation: 30855

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

http://code.google.com/apis/maps/documentation/directions/

Upvotes: 13

Related Questions