Transportation Features in Google Maps
The Google Maps API has recently seen the addition of some new transportation related features including traffic overlays and driving directions. Traffic support and driving directions are both new features of the API and require the use of the v=2.x parameter. Traffic overlays are color-coded polyline data that represents traffic speeds on major highways and can be displayed for more than 30 U.S. cities. This functionality is implemented through the GTrafficOverlay class.
The GTrafficOverlay class, which implements the GOverlay interface, can be added to the Google Maps display through the GMap2.addOverlay( ) method. See the code example below for details on how this is done. In addition, the GTrafficOverlay class has two methods, hide( ) and show( ) which are used to toggle the display of traffic information.
Driving direction functionality is provided through the GDirections class and the supporting GStep and GRoute classes. GDirections is used to query for driving directions and display the results on a map and/or text panel. The returned information can include a map with polyline routes and/or textual information. Requests for directions can either be a query string or geographic coordinates.
The constructor used to create new instances of GDirections takes two optional parameters. The map parameter is used to hold the map showing driving directions, and the panel parameter is used to store textual driving directions. Once created, an instance of the GDirections object then uses the load( ) method to issue a new directions query. This query is composed of a string containing a valid directions query. See below for a code example.
There are a number of methods on GDirections that can be used to clear directions results, get the status of a directions request, get the distance of the directions request, obtain routes, and much more.
Each successful result returned by loading a directions query request returns one or more route objects representing the beginning and ending points of a street segment. GRoute objects store information about a single route in the directions result. Various methods are available on GRoute that enable you to get the starting and ending points of the route, get the distance and time of the route, and return an HTML summary of the route.
For more information on the Google Maps API please see our “Introduction to the Google Maps API” e-learning course.