Time for action – adding directions to a map
Perform the following steps to add directions to a map:
Add code to get the JSON direction data from Google. This will be done using a simple HTTP request. When the data is received from Google, it will be in the form of a string. Convert this string into JSON using the
parse
command and send it to a function to be processed:var url = "http://maps.googleapis.com/maps/api/directions/json?origin=Luz-Saint-Sauveur,+France&destination=42.908655,0.145054&sensor=false"; xhr = Titanium.Network.createHTTPClient(); xhr.open('GET',url); xhr.onload = function(){ // Now parse the XML var theData = JSON.parse(this.responseText); addRouteToMap(theData); }; xhr.send();
Create the route processing function
addRouteToMap
. This function will collect the coordinates of the end point of every step and leg on the route. The function will parse the following information from the JSON returned from the call to Google:"routes" : [ "legs" : [ ...