Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps API V3 retrieving data for with and without traffic

Tags:

google-maps

I want to access the duration from one point to another point with traffic and without traffic (see picutre below)

enter image description here

I want to calculate the delay with and without traffic. I can't find the method in the Gogole Maps API v3 that could provide me that data. Or do I have calculate it with other data somehow?

EDIT:
I know how to calculate the duration. The problem is I want to get the duration with traffic and also the duration without traffic (see the picture I provided above), in order to calculate the possible delay time.

EDIT: Please correct me if I'm wrong, but it seems like that's not possible with the Google API V3 according to this link Google maps traffic prediction API V3

Does anyone know how I can get the duration from one point to another? It doesn't have to be by Google though.

like image 770
thadeuszlay Avatar asked Oct 02 '14 08:10

thadeuszlay


2 Answers

You can do it with Distance Matrix API

GET request -

https://maps.googleapis.com/maps/api/distancematrix/json?origins=garching&destinations=hamburg&departure_time=now&key=YOUR_KEY

will give you duration & duration_in_traffic for the route-

{
  "destination_addresses": [
    "Hamburg, Germany"
  ],
  "origin_addresses": [
    "85748 Garching, Germany"
  ],
  "rows": [
    {
      "elements": [
        {
          "distance": {
            "text": "761 km",
            "value": 760831
          },
          "duration": {
            "text": "7 hours 1 min",
            "value": 25242
          },
          "duration_in_traffic": {
            "text": "6 hours 42 mins",
            "value": 24145
          },
          "status": "OK"
        }
      ]
    }
  ],
  "status": "OK"
}

The departure_time is listed as an optional parameter in the documentation but it is required if you want to see a duration_in_traffic estimate.

Hope this helps.

like image 148
charsi Avatar answered Sep 30 '22 06:09

charsi


There is a traffic layer from Google Maps JS API.

You need to add to your map the traffic layer:

var trafficLayer = new google.maps.TrafficLayer();
trafficLayer.setMap(map);

Here there is a live example.

And if you want to calculate the duration, there is a distance matrix API from Google and in this question from Stackoverflow, you can see the answer.

In Direction Service you need to give this field:

{
  origin: LatLng | String,
  destination: LatLng | String,
  travelMode: TravelMode,
  transitOptions: TransitOptions,
  unitSystem: UnitSystem,
  durationInTraffic: Boolean, /* this is with traffic or without */
  waypoints[]: DirectionsWaypoint,
  optimizeWaypoints: Boolean,
  provideRouteAlternatives: Boolean,
  avoidHighways: Boolean,
  avoidTolls: Boolean
  region: String
}

You can try first with the traffic and save the duration and then you can close the traffic option and save it. So you can see the difference between them.

like image 40
xmux Avatar answered Sep 30 '22 04:09

xmux