Design Shack showcases inspiring web design, alongside resources and tutorials for you to succeed in the same way. It is carefully curated and edited by Josh Johnson and David Appleyard.
There are times when one needs to find out which part of the world a particular visitor is coming from. There are plenty of IP-to-Location lookup providers out there, who offer this service at a reasonable cost (depending on how much detail you require).
Google’s AJAX Maps API offers this look up for you free of cost (so long your needs are non-commercial). You can even use the latitude and longitude information returned by the API to plot the user’s location on a Google Map. Nifty eh? Let’s now look at a simple example – we will be detecting the user’s location based on his IP address and rendering it on a map.
Before we begin, you might want to check out the demo.
You will first need to obtain an AJAX Search API Key (it’s free, just sign up using your domain name, and you will be given a unique key, which is applicable for all pages hosted within your domain). Once you have that ready, fire up your favorite editor. We don’t need any server side code to be able to use the Maps API – so a plain HTML document is enough.
First, let’s place the “hooks” in the page, where the map and a caption will appear.
<div id="map"></div> <div id="location"></div>
The Google AJAX API consists of a collection of individual APIs. By using a <script> tag in the head of the document, we will first load a single method, google.load, using which we can load any or all of the APIs we need individually.
<script type="text/javascript" src="http://www.google.com/jsapi?key=<em>YOUR_API_KEY</em>"> </script>
In our case, we just need the Maps API, so we do:
google.load("maps", "2", {callback: initialize});
The load function above takes the API name, its version and an optional callback function (which is called once the Maps API has finished loading) as parameters. In the init() function, we will first define a default location to be displayed on the map, in case the Maps API is not able to decode the IP address of the visitor. Since the Maps API basically matches the user’s IP address to a location, there might be instances when this mapping is not possible, and hence the need for a default.
var zoom = 4; var latlng = new google.maps.LatLng(50, -10); var location = "Showing default location for map.";
Next, we check if the client location was filled in by the API loader. If properly filled, the google.loader.ClientLocation object has the following properities:
Using this information, we can easily load a map, centred on the user’s location.
document.getElementById("location").innerHTML = location; // location as text caption var map = new google.maps.Map2(document.getElementById('map')); map.setCenter(latlng, zoom); map.addControl(new GLargeMapControl()); map.addControl(new GMapTypeControl());
That’s it, see it in action. As I have already stated above, the client location information returned by this API is not perfect, as IP mapping can often be tricky, and might result in incorrect, or partially accurate information. While you should consider going for an enterprise ready solution for serious uses, I hope this API will be handy for your personal needs!
I had forgotten that you could do this with the Google API, and was beginning to implement a similar system for my employer (http://africanchildrenschoir.com) when I happened accross this post.
You mention in the final paragraph that IP location is not perfect – one instance I have discovered where it really is not perfect is if you are using a mobile device. My cellphone company, Rogers in Canada provide a Toronto located IP, regardless where you are accessing their HSPDA network from. This would obviously lead to misleading results, and would be a bigger problem if your site has a large number of mobile clients.
Thanks…
I had forgotten that you could do this with the Google API, and was beginning to implement a similar system for my employer (http://africanchildrenschoir.com) when I happened accross this post.
nice design Teknoloji ve Tasarım
This post is very good for me.. i can get many tips and trick on this blog.. thank’s for u’r information my friend! this is my first time to visiting to your blog..
It doesnt work. I’m in Buenos Aires and it show the default location.