Looking up for pizza delivery establishments

With these location-based services, not only can we get directions, but we can also look up establishments that are within our area. There are several services that are available for looking up restaurants, stores, and food delivery establishments; some of these include Yelp and Yahoo Local Search.

In our example, we will use the Yahoo Local Search API, which also has support for returning results in the JSON format. We will also follow the same technique as we did for getting directions. We must refer to the Yahoo Local Search Web Services API for the parameters and format of the URL request. You can check out the documentation in the following URL:

http://developer.yahoo.com/search/local/V3/localSearch.html

Based on the documentation, we need to pass the following parameters to successfully get a result:

Parameter

Description

Value

appid

This is the application ID. You can register to get your own at http://developer.yahoo.com/wsregapp/

YahooDemo (for demo purposes only)

query

This is the string used for searching.

For example: Pizza, Car, and Cinema

location/zip

This is the address where the requester is located. Yahoo Local Search only works in the US.

Los Angeles, CA

output

XML, JSON, or PHP.

By default, it outputs as XML

With this information, let us create our local search script and look for pizza delivery establishments. Our script will accept two arguments, query or search string, and our address.

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

var query = system.args[1];
var address = system.args[2];

Then, we open a new page using our request URL and dynamically add our parameters.

page.open(encodeURI('http://local.yahooapis.com/LocalSearchService/V3/localSearch' + '?appid=YahooDemo'+ '&query=' + query+ '&location=' + address+ '&results=2&output=json'), function (status) {
  if (status == 'success') {

After we successfully retrieve the result, we can parse and traverse the list. Please refer to Yahoo!'s documentation regarding the output format for complete reference. We will only use some of the data as part of our script, such as the address, business URL, or phone number, as shown in the following code:

    var localsearch = JSON.parse(page.plainText);
    if(localsearch.ResultSet.totalResultsAvailable == '0'){
      console.log('No result found.'),
    } else {
      console.log("Top results are:");
      var results = localsearch.ResultSet.Result;
      results.forEach(function (result) {
        console.log(result.Title);
        console.log(result.Address + ", "+ result.City + ", "+ result.State);
        console.log(result.Phone);
        console.log(result.BusinessUrl);
        console.log("Ratings : " + result.Rating.AverageRating);
        console.log("");
      });
    }

We can determine whether or not the query found is something related to our query. In the preceding code; we can check the total results available. If we have a result, we can iterate each item in the result property, as shown in the following screenshot:

Looking up for pizza delivery establishments
..................Content has been hidden....................

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