Getting driving directions

With the vast availability of map services from Google, Bing, Yahoo!, and even MapQuest, getting directions is very easy. We will explore how to use the Google Directions API to generate driving directions from one place to another.

Note

First, we need the request URL of the Google Directions API. Based on the documentation, the request URL is as follows:

http://maps.googleapis.com/maps/api/directions/output?parameters

The output can be either JSON or XML, depending on which format of output we want our result to be. We will be using JSON for our example. There are also required parameters such as those described in the following table:

Parameter

Description

origin

Beginning address

destination

Destination address

sensor

Indicator of whether or not the request is coming from a device or a location sensor; it can be either true or false

So, if we are going to get directions from Los Angeles, California to San Francisco, California, we need to use the following request URL:

http://maps.googleapis.com/maps/api/directions/json?origin=Los Angeles, California&destination=San Francisco, California&sensor=false

We will be setting the sensor to false since PhantomJS is not a device or location sensor. So, let us implement that in our script. First, we need to accept two parameters—origin and destination.

var page = require('webpage').create();
var system = require('system'),

var origin = system.args[1];
var destination = system.args[2];

Then, we open a new page using our request URL, but instead of hardcoding the origin and destination, we will be dynamically adding our parameters to our URL.

page.open(encodeURI('http://maps.googleapis.com/maps/'+ 'api/directions/json'+ '?origin=' + origin+ '&destination=' + destination+ '&sensor=false'), function (status) {
  if (status === 'success') {

If we successfully load our page, we can parse and convert the page content into a JSON object.

    var direction = JSON.parse(page.plainText);

Our next step is to traverse the JSON object and get directions. But, before we can do that, we must know the structure of the Google Direction API JSON result, as shown in the following screenshot:

Getting driving directions

The preceding screenshot is the JSON result structure of the Google Direction API. It is just a subset of the structure of the result data and this will be enough for our example. You can get the complete JSON format in the Google Direction API documentation of the JSON result at https://developers.google.com/maps/documentation/directions/#JSON

We can get the status of the request using the status property and, if we get the status of OK, the direction is available for us to parse.

    if(direction.status == 'OK'){

The result may contain multiple routes and each route may have multiple legs of direction. We will only get the first route and the first leg. Based on the result, we first get the starting address. This may contain the full address. We get that using the start_address property (see the following line of code):

      console.log("A - " + direction.routes[0].legs[0].start_address);

Next, we traverse each step under the first leg of the result. We extract the steps property array and then loop for each item.

      var steps = direction.routes[0].legs[0].steps;
      var step_ctr = 0;
      steps.forEach(function (step) {
        step_ctr++;
        var instruction = step.html_instructions.replace(/(<([^>]+)>)/ig, "");
        console.log(step_ctr + " - " + instruction);
      });

The directions can be found in the html_instructions property of each step, so we output that as part of our step-by-step direction instructions. As per the property name html_instructions, this property contains HTML tags that Google intends to be visualized in a browser. We can remove all the HTML tags within the html_instructions value using a simple replace action.

Within this loop, we can add more details, such as the distance for this step and the estimated duration. Lastly, to complete our direction instructions, we output the end address or the destination.

    console.log("B - " + direction.routes[0].legs[0].end_address);

We can also add something like checking whether or not the status of the result is OK to determine whether or not to display an error message. Let's execute our script as follows:

Getting driving directions

You can get more information about the Google Direction API in the following URL:

https://developers.google.com/maps/documentation/directions/

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.181.163