简体   繁体   中英

Duration in traffic google api

I am trying to find the travel duration from origin to destination using google's direction services api. I need to find out travel duration according to traffic conditions. I tried using javascript and it returns the duration in traffic value. But when i tried the same problem in java it doesn't seem to return the duration in traffic value. So how to obtain duration in traffic using direction services api of google?

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 have pasted the url 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 required to travel under traffic conditions. What is the equivalent process in java?

 var request = {

      origin:"Wollongong, Australia",
      destination:"Sydney,Australia",
      travelMode: google.maps.DirectionsTravelMode.DRIVING,
      provideRouteAlternatives: true,
      durationInTraffic: true
    };

Take a look at the duration_in_traffic definition under the Legs section on the maps documentation page:

  1. The directions request includes a departure_time parameter set to a value within a few minutes of the current time.
  2. The request includes a valid Maps for Business client and signature parameter.
  3. Traffic conditions are available for the requested route.
  4. The directions request does not include stopover waypoints.

Maybe not all of the 4 conditions they list are being met?

There is also a JSON example on that page w/ a sample url you can use if it helps any.

In Google API URL add &departure_time=now it returns duration_in_traffic time .now means currentTime or you can put millisecond time in place of now.

if you adding waypoints then it will not returns duration_in_traffic

because google Api mentioned The directions request does not include stopover waypoints.

but if you adding waypoints using via:lat,long then it will returns duration in traffic because google API will not consider waypoints as stopover

I am trying to find the travel duration from origin to destination using google's direction services api. I need to find out travel duration according to traffic conditions. I tried using javascript and it returns the duration in traffic value. But when i tried the same problem in java it doesn't seem to return the duration in traffic value. So how to obtain duration in traffic using direction services api of google?

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 have pasted the url 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 required to travel under traffic conditions. What is the equivalent process in java?

 var request = {

      origin:"Wollongong, Australia",
      destination:"Sydney,Australia",
      travelMode: google.maps.DirectionsTravelMode.DRIVING,
      provideRouteAlternatives: true,
      durationInTraffic: true
    };

If you set departure_time you can get duration_in_traffic for each your legs , but if you want duration_in_traffic for each steps you should use distance_matrix API , and merge this two . In distance google API you need departure_time set .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM