Tuesday, June 15, 2010

Geolocation Made easy

Geolocation is the process through which the geographical position related to an user connected to Internet can be obtained basing on the information provided by its IP address. Knowing the physical location is an important thing, since, using it, a website could provide more detailed information to the user (such as hotels or restaurants close to his/her area, targeted ads, maps and other information related to the traffic or the weather conditions), and, moreover, it is very useful and helpful when used on mobile devices such as laptops, smartphones, or PDAs. IP addresses are assigned to the Internet users by the service provider and they are assigned all over the world basing on geographical areas: basing on this information, the physical location can be easily associated to each IP address (in a similar way to what happens for the telephone numbers). In order to do that operation, a database with the IP <-> location association is required; on the web there are several companies that provides this type of service, either free of charge or not. One of these companies is Maxmind, with its GeoIP technology (http://www.maxmind.com/app/ip-location). By visiting a website powered with GeoIP, it is possible to obtain detailed information about the user’s location, such as: country, region, city, postal code, area code, latitude, longitude, connection speed, ISP, company name, and domain name. MaxMind provides geolocation APIs both for Linux and Windows, and for many different languages, such as C, C#, Java, Pascal, Perl, PHP, Python, Ruby, and so on.

GeoIP Javascript Web Service

We will see now the Javascript code provided by MaxMind at THIS address:


Country Code:


Country Name:


City:


Region:

Region Name:

Latitude:

Longitude:


Postal Code:

Just copy this script in an html page and open it with your favorite web browser: you will see the relevant information related to your geographical position. It’s really very easy, right? GeoIP JavaScript is a service offered by MaxMind to return the Country, Region, City, Latitude, and Longitude for your web visitors. It uses Javascript and is very easy to program, works on both static and dynamically served web pages. You can even produce a graphical output by means of the Google Maps application (visit the website: http://code.google.com/intl/it-IT/apis/maps/), centering it providing the coordinates (latitude and longitude) obtained with the MaxMind javascript. It should be noticed that IP geo-location does not always provide the exact geographical position: an approximation is always present depending on the provider’s actual position; moreover, if you are using a private network connection (that is, a VPN) you might not obtain the right information (actually misleading information will be shown, probably).

W3C geolocation API

Support for geolocation is provided also by W3C through a specific set of APIs (specifications are available at THIS site. Not all of the Internet browsers support it, as well, but Firefox (starting from release 3.5) is one of those that provide support for it. You can check the coordinates (latitude and longitude) corresponding to your IP address position by running the below shown script. It should also be noticed that, in order to protect your privacy, the browser will ask you for permission when requesting the position’s coordinates (you may accept or not accept it, of course):


Obtaining a map with Google Maps

We can now put all stuff together and, knowing the current geographical position (expressed by latitude and longitude), we can display the map corresponding to that position. That is achieved through the Google Maps service, and you should prior register and obtain a key for non-commercial use (insert your key in place of the "InsertHereYourKey" word). The following script starts displaying a fixed map centered on Italy (you can change lat and lon to display another geographical area); then, if the user agrees to do it, it shows the current position coordinates in a dialog box. Finally, it shows the map corresponding to that geographical position:



The following two images shows the output of the previous script assuming the Internet user is located in the Florence area:

No comments:

Post a Comment