Ahmed.Ouda
Ahmed.Ouda

Reputation: 33

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.

Upvotes: 2

Views: 13544

Answers (3)

public String getTimeTaken(Location source, Location dest) {


    double meter = source.distanceTo(dest);

    double kms = meter / 1000;

    double kms_per_min = 0.5;

    double mins_taken = kms / kms_per_min;

    int totalMinutes = (int) mins_taken;

    Log.d("ResponseT","meter :"+meter+ " kms : "+kms+" mins :"+mins_taken);

    if (totalMinutes<60)
    {
        return ""+totalMinutes+" mins";
    }else {
        String minutes = Integer.toString(totalMinutes % 60);
        minutes = minutes.length() == 1 ? "0" + minutes : minutes;
        return (totalMinutes / 60) + " hour " + minutes +"mins";

    }


}

call this method like

 binding.estimationTime.setText("" + getTimeTaken(deliveryboy_location, userLocation) );

Upvotes: 0

Philipp
Philipp

Reputation: 494

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

  • roads
  • speed limit
  • traffic
  • graphs

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;

Upvotes: 6

Yomesh Mistri
Yomesh Mistri

Reputation: 19

 @Override
    protected void onPostExecute(List<List<HashMap<String, String>>> result) {
        ArrayList<LatLng> points = null;
        PolylineOptions lineOptions = null;
        MarkerOptions markerOptions = new MarkerOptions();
        String distance = "";
        String duration = "";


        if (result.size() < 1) {
            Toast.makeText(getBaseContext(), "No Points", Toast.LENGTH_SHORT).show();
            return;
        }


        // Traversing through all the routes
        for (int i = 0; i < result.size(); i++) {
            points = new ArrayList<LatLng>();
            lineOptions = new PolylineOptions();

            // Fetching i-th route
            List<HashMap<String, String>> path = result.get(i);

            // Fetching all the points in i-th route
            for (int j = 0; j < path.size(); j++) {
                HashMap<String, String> point = path.get(j);

                if (j == 0) {    // Get distance from the list
                    distance = (String) point.get("distance");
                    continue;
                } else if (j == 1) { // Get duration from the list
                    duration = (String) point.get("duration");
                    continue;
                }

                double lat = Double.parseDouble(point.get("lat"));
                double lng = Double.parseDouble(point.get("lng"));
                LatLng position = new LatLng(lat, lng);

                points.add(position);
            }

            // Adding all the points in the route to LineOptions
            lineOptions.addAll(points);
            lineOptions.width(10);
            lineOptions.color(Color.GREEN);

        }

        text.setText("Distance:" + distance + ", Duration:" + duration);
        Log.e("Final distance", "Distance:" + distance + ", Duration:" + duration);
        // Drawing polyline in the Google Map for the i-th route
        mMap.addPolyline(lineOptions);
    }
}

Upvotes: 0

Related Questions