How To Get Geographical Location Details from IP and Show Them on a Map
Posted Under: Google Maps, How To, PHP, Programming, REST, SOAP, VU, Web Service
Our first assignment for Intelligent Web Applications course at VU was to transform a WSDL web service (either GeoIPService or Fraudlabs IP2Location Webservice) to a RESTful web service and display the result using either Google Maps or Yahoo Maps.
Me and my project mate Mies developed 2 different versions for this assignment.
- Version 1 – uses the GeoIPService, the Ruby programming language, Sinatra, soap4r, the jquery Javascript framework and the Yahoo Maps API and can be found here:
iwa-wsdl2ruby - Version 2 – uses the Fraudlabs IP2Location webservice, the PHP programming language, nusoap, Javascript and the Google Maps API and can be found here:
DisplayGeographicalLocation.html
Version 1:
Since Version 1 was fully developed by Mies, I will not post the source code for this here. Maybe he can give a link to his code if he wants. So here is the explanation for the version 1:
The GeoIP WSDL-to-REST service is built using Sinatra, soap4r, the Ruby programming language, Yahoo Maps and the jquery javascript framework. Sinatra is an open source web application framework and domain specific language written in Ruby. As opposed to other web frameworks such as Rails (also in Ruby) or Django (written in Python), Sinatra is very minimal in its design and implementation. URL Routes in Sinatra are HTTP methods combined with some pattern matching and logic. The simplest URL route and thus web application in Sinatra is:
get '/' do
"Hello World"
end
which basically prints out “Hello World” if invoked via the web server using the root url (http://localhost/). As such Sinatra is ideal for building small REST services, API’s and web hooks, and thus a perfect candidate for completing this assignment.
All application logic resides in the ‘main.rb’ file. Using the soap4r library (an implementation of Soap 1.1 in Ruby), a request object is created using the provided .wsdl file. The IP address as provided by the user is passed as parameter to the getGeoIP method of the request object. Second a result object (in fact a key-value pair, or dictionary) is created using the getGeoIPResult method, invoked upon the same request object. The actual country name is the value of the CountryName key within the result key-value pair.
Two forms of URL routes are provided, one for the html representation and a second route for the REST API. The html representation is a simple .erb (a ruby templating language) template with a form. The IP address can be submitted and the result is displayed.
The second URL route provides the methods for the RESTful API. The following methods are supported: /api/location.
Current options are .xml and .json
For instance the url:
/api/location.xml?ip=62.69.184.53 will render a response NETHERLANDS
whereas
the location.json?ip=62.69.184.53 equivalent will render a {”CountryName”:”NETHERLANDS”} response.
The ajax response and yahoo maps calls are handled using jquery; a javascript framework.
Figure 1 displays an overview of the application’s architecture:
The final web application is deployed using Heroku, a cloud-based hosting environment for ruby applications at http://iwa-wsdl2ruby.heroku.com/. The initial version used the Google Maps Geocoding API for requesting a latitude and longitude and subsequently render the actual map. However as Heroku uses a reverse proxy (Nginx) and http caching layer (Varnish) to service and load balance requests, and being able to dynamically scale deployed web applications, the IP address used for this web application is not static. As such the Google Maps API key can not be coupled to this web application and thus an error message appears and the actual map is not rendered. The Yahoo Maps API is less strict in this sense about its usage and thus we ended up using this service in rendering the final map.
Version 2:
This version of the application makes use of Javascript in the front-end and PHP at the back-end and only works for IE for now. This can be avoided by using a framework like prototypejs.
When the user browses to DisplayGeographicalLocation.html 2 new Ajax objects are created as the page is loaded(one for getting IP asynchronously and the other for getting the geographical location asynchronously).
The logic of the application resides in ipToMap function. If the ip input text field is empty, it makes an HTTP GET request to getUserIP.php which returns the user IP to the client.
The Javascript code for this would look like this:
The PHP code for getting user ip is as below:
After the ip is successfully assigned to the ip input text field, a new http GET request is made to getGeoLoc.php and ip is given as a parameter.
This php file makes use of nusoap library in order to create a soap client using the WSDL file provided by fraudlabs: ip2locationwebservice.asmx?wsdl and to call the fraudlabs IP2Location webservice with a message including the ip and licence key needed to use this webservice.
A sample call to getGeoLoc.php and its result would look like this:
And the source code for getGeoLoc.php which turns WSDL webservice into REST.
The result is then processed and turned into XML format within getGeoLoc.php and returned to the ajaxObject as a responseText. This text is than recognized as an XMLDOM and parsed and the latitude and longtitude are retrieved. These latitude and longtitude variables are then used for creating a map using the Google Maps API.
Please note that the free version of fraudlabs includes only 90 calls to their webservice per month, so when the credits are finished the result will not be accurate, it will return a latitude of 0 and longtitude of 0.
These examples are bound to external services though.
In practice, if it is a crucial functionality for your projects, I would recommend keeping the ip to geographical details on your database and write a service of your own, so that when these external services are down or you are out of credits, you wouldn’t be angry at other parties :)


Reader Comments
Hi,
I happened to see your post find it quite informative. I would like to share a link where a software engineer has shared a tip on “How to get geographical location of an IP address in PHP?”. I am sharing it for “version 2 assignment” which you have explained above.
Here is the link:
http://www.mindfiresolutions.com/How-to-get-geographical-location-of-an-IP-address-in-PHP-815.php
Hope you find it useful and of assistance.
Thanks,
Bijayani
Hi! Bijayani..
Thank you very much for posting that link, it’s really working great, I got really tired for looking this script in internet, once again thank you