Access Google's Traffic Data through a Web Service
Asked Answered
F

10

89

Is there a way I can access traffic data that Google provides through a web service?

There seems to be a GTrafficOverlay that puts traffic on top of a route on an embedded google map, but no direct web service that I can consume to, say, give the source and the destination and find the traffic between them?

Is there any other source I can get this data from?

Fungosity answered 5/1, 2011 at 3:45 Comment(1)
developers.google.com/maps/documentation/javascript/examples/… appears to be a recent addition.Agnostic
I
52

UPDATE (May 2022): From @AbdullahTahan:

Now google has this feature but it's paid and costs 0.01$ per request check this https://developers.google.com/maps/documentation/distance-matrix/distance-matrix#distance-matrix-advanced

UPDATE (March 2016): A lot has happened since this answer was written in 2011, but the core points appear to hold up: You won't find raw traffic data in free API services (at least not for the U.S., and probably not most other places). But if you don't mind paying a bit and/or if you just need things like "travel time for a specific route taking traffic into consideration" you have options. @Anto's answer, for example, points to Google's Maps For Work as a paid API service that allows you to get travel times taking traffic into consideration.

ORIGINAL ANSWER: There is no way (or at least no reasonably easy and convenient way) to get the raw traffic data from Google Maps Javascript API v3. Even if you could do it, doing so is likely to violate some clause in the Terms Of Service for Google Maps. You would have to get this information from another service. I doubt there is a free service that provides this information at the current time, but I would love it if someone proved me wrong on that.

As @crdzoba points out, Bing Maps API exposes some traffic data. Perhaps that can fill your needs. It's not clear from the documentation how much traffic data that exposes as it's only data about "incidents". Slow traffic due to construction would be in there, but it's not obvious to me whether slow traffic due simply to volume would be.

Interchangeable answered 5/6, 2011 at 20:39 Comment(0)
S
15

Apparently the information is available using the Google Directions API in its professional edition Maps for work. According to the API's documentation:

Note: Maps for Work users must include client and signature parameters with their requests instead of a key.
[...]
duration_in_traffic indicates the total duration of this leg, taking into account current traffic conditions. The duration in traffic will only be returned if all of the following are true:

  • The directions request includes a departure_time parameter set to a value within a few minutes of the current time.
  • The request includes a valid Google Maps API for Work client and signature parameter.
  • Traffic conditions are available for the requested route.
  • The directions request does not include stopover waypoints.
Shoffner answered 1/12, 2014 at 15:2 Comment(0)
I
15

You might want to take a look at HERE MAP SERVICE. They have direct traffic data you can use, which is exactly what you need: https://developer.here.com/api-explorer/rest/traffic/traffic-flow-bounding-box

For example, by querying an area of interest, you might get something like this:

{
  "RWS": [
    {
      "RW": [
        {
          "FIS": [
            {
              "FI": [
                {
                  "TMC": {
                    "PC": 32483,
                    "DE": "SOHO",
                    "QD": "+",
                    "LE": 0.71682
                  },
                  "CF": [
                    {
                      "TY": "TR",
                      "SP": 9.1,
                      "SU": 9.1,
                      "FF": 17,
                      "JF": 3.2911,
                      "CN": 0.9
                    }
                  ]
                }
              ]
            }
          ],
....

This example shows a current average speed SU of 9.1, where the free flow speed FF would be 17. The Jam factor JF is 3.3, which is still considered free flow but getting sluggish. The units used (miles/km) can be defined in the API call. To avoid dealing with TMC locations, you can ask for geocoordinates of the road segments by adding responseattributes=sh in the request.

The abbreviations used can be found here Interpreting HERE Maps real-time traffic tags:

  • "RWS" - A list of Roadway (RW) items
  • "RW" = This is the composite item for flow across an entire roadway. A roadway item will be present for each roadway with traffic flow information available
  • "FIS" = A list of Flow Item (FI) elements
  • "FI" = A single flow item
  • "TMC" = An ordered collection of TMC locations
  • "PC" = Point TMC Location Code
  • "DE" = Text description of the road
  • "QD" = Queuing direction. '+' or '-'. Note this is the opposite of the travel direction in the fully qualified ID, For example for location 107+03021 the QD would be '-'
  • "LE" = Length of the stretch of road. The units are defined in the file header
  • "CF" = Current Flow. This element contains details about speed and Jam Factor information for the given flow item.
  • "CN" = Confidence, an indication of how the speed was determined. -1.0 road closed. 1.0=100% 0.7-100% Historical Usually a value between .7 and 1.0 "FF" = The free flow speed on this
    stretch of road.
  • "JF" = The number between 0.0 and 10.0 indicating the expected quality of travel. When there is a road closure, the Jam Factor will be 10. As the number approaches 10.0 the quality of travel is getting worse. -1.0 indicates that a Jam Factor could not be calculated
  • "SP" = Speed (based on UNITS) capped by speed limit
  • "SU" = Speed (based on UNITS) not capped by speed limit
  • "TY" = Type information for the given Location Referencing container. This may be freely defined string

Also the source comes from https://developer.here.com/rest-apis/documentation/traffic/topics/additional-parameters.html

Iggie answered 6/6, 2017 at 13:51 Comment(3)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewDeveloping
@Developing Thanks for the suggestions. I have updated my post.Iggie
How do I determine the Fully Qualified ID descripted in the QD field?Cervantez
U
12

Bing Maps API has a REST service that returns traffic info

http://msdn.microsoft.com/en-us/library/hh441725

Unhandled answered 22/5, 2012 at 11:11 Comment(1)
But this means if you want to display the traffic information on a map you have to use bing maps.Malagasy
M
7

Rather than trying to pull the raw traffic data, you can try a different approach. The Google Directions API allows you to query the api with a particular route and returns a JSON string or XML element as a result. This result includes the element - ' duration_in_traffic ' This indicates the total duration of the particular leg of the journey, taking into account current traffic conditions. (for information on 'leg' and other elements of the JSON string returned by the Directions API refer link below)

https://developers.google.com/maps/documentation/directions/#JSON I haven't tried this myself but just something I came across in the documentation.

Mchail answered 10/9, 2014 at 10:4 Comment(2)
It's not working since Google returns the lenght and duration of the trip without considering traffic state. I mean, it returns the same numbers whether it is rush hour or a calm night :(Scathe
It does work if you have a paid account with google maps however the subscription is ridiculously expensive and data usage is restrictive.Mchail
L
5

In India we are using http://www.itrafficnews.com. But the data is posted by the users. I dont think google will provide the data.

Lanneret answered 16/11, 2012 at 3:53 Comment(0)
C
4

Maybe you should have a look at Mapquests Traffic API: http://www.mapquestapi.com/traffic/

The webservice is unfortunately only available for some citys in the US, I think. But probably it solves your problem.

Czardom answered 8/3, 2012 at 4:16 Comment(0)
A
3

I don't think Google will provide this API. And traffic data not only contains the incident data.

Today many online maps show city traffic, but they have not provide the API for the developer. We even don't know where they get the traffic data. Maybe the government has the data.

So I think you could think about it from another direction. For example, there are many social network website out there. Everybody could post the traffic information on the website. We can collection these information to get the traffic status. Or maybe we can create a this type website.

But that type traffic data (talked about above) is not accurate. Even the information provided by human will be wrong.

Luckily I found that my city now provides an Mobile App called "Real-time Bus Information". It could tell the citizen where the bus is now, and when will arrive at the bus station. And I sniff the REST API in this App. The data from REST API give the important data, for example the lat and lon, and also the bus speed. And it's real-time data! So I think we could compute the traffic status from these data (by some programming). Here is some sample data : https://github.com/sp-chenyang/bus/blob/master/sample_data/bjgj_aibang_com_8899_bjgj_php_city_linename_stationno_datatype_type.json

Even the bus data will not enough to compute the accurate real-time traffic status. Incidents, traffic light and other things will affect the traffic status. But I think this is the beginning.

At the end, I think you could try to find whether your city provides these data.

PS: I am always thinking that life will be better for people in the future , but not now.

Alarice answered 12/1, 2014 at 2:28 Comment(0)
G
3

It is possible to get traffic data. Below is my implementation in python. The API has some quota & is not fully free, but good enough for small projects

import requests
import time
import json


while True:

    url = "https://maps.googleapis.com/maps/api/distancematrix/json"

    querystring = {"units":"metric","departure_time":str(int(time.time())),"traffic_model":"best_guess","origins":"ITPL,Bangalore","destinations":"Tin Factory,Bangalore","key":"GetYourKeyHere"}

    headers = {
        'cache-control': "no-cache",
        'postman-token': "something"
        }

    response = requests.request("GET", url, headers=headers, params=querystring)
    d = json.loads(response.text)
    print("On", time.strftime("%I:%M:%S"),"time duration is",d['rows'][0]['elements'][0]['duration']['text'], " & traffic time is ",d['rows'][0]['elements'][0]['duration_in_traffic']['text'])
    time.sleep(1800)
    print(response.text)

Response is :-

{
    "destination_addresses": [
        "Tin Factory, Swamy Vivekananda Rd, Krishna Reddy Industrial Estate, Dooravani Nagar, Bengaluru, Karnataka 560016, India"
    ],
    "origin_addresses": [
        "Whitefield Main Rd, Pattandur Agrahara, Whitefield, Bengaluru, Karnataka 560066, India"
    ],
    "rows": [
        {
            "elements": [
                {
                    "distance": {
                        "text": "10.5 km",
                        "value": 10505
                    },
                    "duration": {
                        "text": "35 mins",
                        "value": 2120
                    },
                    "duration_in_traffic": {
                        "text": "45 mins",
                        "value": 2713
                    },
                    "status": "OK"
                }
            ]
        }
    ],
    "status": "OK"
}

You need to pass "departure_time":str(int(time.time())) is a required query string parameter for traffic information.

Your traffic information would be present in duration_in_traffic.

Refer this documentation for more info.

https://developers.google.com/maps/documentation/distance-matrix/intro#traffic-model
Glide answered 10/10, 2018 at 13:13 Comment(1)
if I need to try it, I only have to replace my key on "GetYourKeyHere" or is there is something else? because I had an error so I want to make sure that am in the right wayBoogiewoogie
T
0

There is a project called Open Traffic which is not fully functional right now but seems to be the right answer in the future.

OpenTraffic is a global data platform to process anonymous positions of vehicles and smartphones into real-time and historical traffic statistics. We're building this in the open, using fully open-source software, with involvement from a growing list of partners.

Tabina answered 26/1, 2020 at 18:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.