Chapter 6. Accessing Location-based Services

Web services using location are becoming more popular. Social media sites use these to gather or display information related to the user's location. With numerous services available, we can use these services and create scripts using PhantomJS to simplify or automate some of the processes. We will discuss some of these possibilities in this chapter.

Checking a location based on IP address

IP geolocation services are widely available now and accessing them gives us new ways to look for new items on the web, such as when we want to get a list of establishments that are close to us. If we can determine the location of a user, we can customize content that is more appropriate to their location.

There are several IP geolocation services that are available and free:

Web URL

Output format

freegeoip.net

CSV, XML, and JSON

ipinfodb.com

XML

hostip.info

HTML and XML

We will use freegeoip.net since it has output in JSON and JSON is much lighter than XML or other formats; aside from this, JSON is natively supported in JavaScript. We can directly parse and manipulate JSON objects readily, without using any third-party library.

Based on the freegeoip.net documentation, to get the geolocation, we must use the following URL format: http://freegeoip.net/(format)/(IP or hostname).

The (format) part of the URL can be one of the following: CSV, XML, or JSON. The (IP or hostname) part of the URL is optional, so we can omit that in our URL. The result in JSON for this service is as follows:

{
  "ip": "119.92.192.222",
  "country_code": "PH",
  "country_name": "Philippines",
  "region_code": "D9",
  "region_name": "Manila",
  "city": "Manila",
  "zipcode": "",
  "latitude": 14.6042,
  "longitude": 120.9822,
  "metro_code": "",
  "areacode": ""
}

Tip

Try opening http://freegeoip.net/json/ in a browser and it will return JSON-formatted data that corresponds to your location.

Based on this JSON data, we can open a page in the PhantomJS script; then, after successful loading, we convert the entire page content, which is in plain text, using the page.plainText property into JSON objects. We can now access the different properties of the JSON object, such as the IP address, city, and country name, as shown in the following code:

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

page.open('http://freegeoip.net/json/', function (status) {
  if (status == 'success') {
    var data = JSON.parse(page.plainText);
    console.log('IP Address: ' + data.ip);
    console.log('Estimated Location: ' + data.city + ", " + data.country_name);
  }
  phantom.exit();
});

In the preceding code, we open the page by passing the freegeoip JSON service URL and then, if the loading is successful, we parse the page content. We can now have different types of outputs from here since we do have access to our geolocation. Running our script will give the output shown in the following screenshot:

Checking a location based on IP address

We can now use this information as input for other location-based services, such as getting directions to another point of interest or looking up certain types of establishments that are near our location.

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

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