Saturday, September 29, 2012

Google Maps -Google Directions API

The directions API allows to calculate directions between addresses known in advance. Using Google Directions API, a developer can search for directions of several modes of transportation such as transit, driving, walking or cycling. Directions may specify origins, destinations, waypoints, either as strings or lat and long coordinates. 

The following is the request format for getting the directions. 
http://maps.googleapis.com/maps/api/directions/output?parameters

To get the response in JSON format, the output value need to be json. To get in XML format,
it need to be xml.

Below are the required parameters for this request: 
origin: the address or textual latitude/longitude value from which you wish to calculate the directions. If we pass the address as string, directions will geocode the string and convert it to a latitude and longitude coordinate to calculate the directions. 
destination: the address or textual latitude/longitude value to which you wish to calculate the directions.
sensor : indicates whether the request comes from a device which has local sensor.

Below are few optional parameters which could be of a developers interest: 
mode: specifies the mode of transport when calculating the directions. 
waypoints: waypoints are specified locations to be included while calculating directions. 
alternatives: if multiple alternate routes are required in output
avoid: specifies certain features to be avoided, the supported values are: tolls, highways
language: the output language
units: units of measurement
region: region code. specified as ccTLD( top level domain) 
departure_time: desired departure time. This parameter is considered for transit mode alone.
arrival_time : desired arrival time in transit mode. 

Below is a sample request to reach to Bangalore from Kerala without using any toll roads

http://maps.googleapis.com/maps/api/directions/json?origin=Kerala&destination=Bangalore&sensor=false&avoid=tolls&mode=driving

When to use regions: Regions are mostly required while trying to get the directions for a smaller region where the name of the origin or destination could be there in multiple countries. for e.g.  a directions request from "Toldeo" to "Madrid" returns a result when region is set to es as "Toledo" is interpreted as the Spanish city:
However, A directions for "Toledo" to "Madrid" sent to with no region parameter will not return any results, since "Toledo" is interpreted as the city in Ohio:

Understanding the response format: 



 
 

No comments:

Post a Comment