javaandroidgoogle-maps-android-api-2google-distancematrix-api

Calculate the duration or expected drive time between two latitude, longitude points


I want to calculate the duration between two points. I know there is Google Maps API but I don't want to use it, I need an equation to do it. There is one calculate the distance I need one for the duration of drive time.


Solution

  • Well, take a guess how you can achieve that without the knowledge of

    Pretty hard. I see 2 solutions:

    Use the Google Distance Matrix API

    https://developers.google.com/maps/documentation/distance-matrix/start

    https://maps.googleapis.com/maps/api/distancematrix/json?origins=my_origins&destinations=my_destinations&departure_time=now

    Estimate with Speed

    Location loc1 = new Location("");
    loc1.setLatitude(lat1);
    loc1.setLongitude(lon1);
    
    Location loc2 = new Location("");
    loc2.setLatitude(lat2);
    loc2.setLongitude(lon2);
    
    float distance = loc1.distanceTo(loc2);
    
    int speed=30;
    float time = distance/speed;