Duration in google traffic api

I am trying to find the duration of travel from source to destination using google api services. I need to find out the duration of the trip in accordance with traffic conditions. I tried using javascript and it returns the duration of the traffic. But when I tried the same problem in java, it does not seem to return the duration of the traffic. So, how to get the duration in traffic using google api referral services?

URL url = new URL ("http://maps.googleapis.com/maps/api/directions/json?origin=" + URLEncoder.encode(origin, "UTF-8") +"&destination="+ URLEncoder.encode(destination, "UTF-8") +"&waypoints=optimize:true|Bagbazar,Kathmandu|Thapathali,Kathmandu|Kamal+Pokhari,Kathmandu"+"&sensor=false");

Above, I inserted the url that I used to send to google server. Below is the java script code, in the following code I can set durationInTraffic: true, which returns the time it takes to move through the traffic conditions. What is an equivalent process in java?

  var request = { origin:"Wollongong, Australia", destination:"Sydney,Australia", travelMode: google.maps.DirectionsTravelMode.DRIVING, provideRouteAlternatives: true, durationInTraffic: true }; 
+4
source share
4 answers

See the definition of duration_in_traffic in the Legs section of the map documentation page:

  • The direction request includes the departure_time parameter, set to a value within a few minutes of the current time.
  • The request includes a valid client parameter and a signature parameter for Microsoft.
  • Traffic conditions are available for the requested route.
  • A referral request does not include waypoints.

Perhaps not all 4 conditions they list meet?

There is also a JSON example on this page with a sample URL that you can use if it helps anyone.

+1
source

In the google API url add & departure_time = now it returns duration_in_traffic time. now means currentTime, or you can enter millisecond time instead.

if you add waypoints then it will not return duration_in_traffic

as mentioned google api. Route request does not include waypoints.

but if you add waypoints using: lat, long, then this will return the duration in the traffic, because the Google API will not treat waypoints as a stop

0
source

Traffic duration is available only to premium users. For the duration of the traffic, you must add the following parameters 1- departure_time = now 2- key = YOUR_API_KEY or client_id = CLIENT_ID & signature = SIGNATURE for premium users

0
source

If you set the departure time, you can get duration_in_traffic for each of your legs, but if you want duration_in_traffic for each step you used the distance_matrix API and combined the two. In the Google Remote API, you need to set the departure_time parameter.

0
source

Source: https://habr.com/ru/post/1495885/


All Articles