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

不羁的心 提交于 2019-11-28 04:25:47

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/

Yaqub Ahmad
    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

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

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

   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();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!