Find web visitor’s location automatically with javascript and Google APIs
Reading time: About 3 minutes
Update June 1, 2009: This works great in the United States and has mixed results elsewhere. I’m working to see if I can change that. If your primary customer base is in the US, use this confidently. If your primary customer base is elsewhere, use in development stage only.
What if you could automatically identify where your website visitors came from? Imagine the possibilities of serving web visitors locale–specific information without the slightest effort on their part. Now that’s what I call usability, accessibility, and pure marketing awesomeness all wrapped in one package.
This tutorial finds a web visitor’s location based on their IP address. Keep in mind that IP to location translation isn’t an exact science. After all, would you want the websites you visit to be able to find your exact home address? That’s a scary thought.
What this tutorial will do is at least provide an idea of a visitor’s exact city (in most cases). You also get the visitor’s approximated latitude and longitude, as well as his or her’s state, country, and country code.
If you’re the type who likes to take apart source code, skip the tutorial and see a sample with source code.
Who provides IP address to location translation for FREE!?
Google does. Whenever you use Google’s jsapi script to dynamically load a Google AJAX API, Google automatically populates google.loader.ClientLocation, which has all the juicy details about the web visitor’s location.
Surprised? It’s a little known (and badass) fact.
Step 1: Load the Google AJAX API script
If you haven’t already, signup for a free Google API key.
Simply including the following
jsapiscript gives you the web visitor’s location details in google.loader.ClientLocation.
1<script type="text/javascript" src="http://www.google.com/jsapi?key=API_KEY_GOES_HERE"></script>Step 2: Extract the web visitor’s location from google.loader.ClientLocation
Add this javascript inside its own
<script type="text/javascript">tag to extract the web visitor’s location details from google.loader.ClientLocation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 if(google.loader.ClientLocation) { visitor_lat = google.loader.ClientLocation.latitude; visitor_lon = google.loader.ClientLocation.longitude; visitor_city = google.loader.ClientLocation.address.city; visitor_region = google.loader.ClientLocation.address.region; visitor_country = google.loader.ClientLocation.address.country; visitor_countrycode = google.loader.ClientLocation.address.country_code; } else { // ClientLocation not found or not populated // so perform error handling }Step 3: Put it all together in a webpage
Here’s a sample webpage that displays the user’s location. Try it out!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>Get web visitor's location</title> <meta name="robots" value="none" /> </head> <body> <div id="yourinfo"></div> <script type="text/javascript" src="http://www.google.com/jsapi?key=ABQIAAAAp04yNttlQq-7b4aZI_jL5hQYPm-xtd00hTQOC0OXpAMO40FHAxQMnH50uBbWoKVHwgpklyirDEregg"></script> <script type="text/javascript"> if(google.loader.ClientLocation) { visitor_lat = google.loader.ClientLocation.latitude; visitor_lon = google.loader.ClientLocation.longitude; visitor_city = google.loader.ClientLocation.address.city; visitor_region = google.loader.ClientLocation.address.region; visitor_country = google.loader.ClientLocation.address.country; visitor_countrycode = google.loader.ClientLocation.address.country_code; document.getElementById('yourinfo').innerHTML = '<p>Lat/Lon: ' + visitor_lat + ' / ' + visitor_lon + '</p><p>Location: ' + visitor_city + ', ' + visitor_region + ', ' + visitor_country + ' (' + visitor_countrycode + ')</p>'; } else { document.getElementById('yourinfo').innerHTML = '<p>Whoops!</p>'; } </script> </body> </html>That’s all you have to do!
Can you believe it is that simple? I can’t believe I’m giving this information gold mine away so freely!
It’s time to use your imagination and put this information to use! Take it a step further with the Google Maps API and read my article How to calculate distance with Javascript and Google Maps API! Be creative and have fun! I did when I used it to create <revealsecrets>Nearby Tweets</revealsecrets>.
![]()
46 comments skip to comment form
FokusLop said— 15 hours later
Mike Mella said— 2 days later
Paul said— 2 days later
Brian Cray said— 2 days later
Mike Mella said— 2 days later
Bill said— 2 days later
Brian Cray said— 2 days later
Bill said— 2 days later
Richard said— 3 days later
Desi Blog said— 3 days later
Stephen Cronin said— 3 days later
Ash said— 3 days later
Ayush said— 3 days later
Brian Cray said— 3 days later
Russell said— 3 days later
Bill said— 3 days later
N GowriSankar said— 4 days later
jQuery Tutorials said— 4 days later
Justin said— 1 week later
cms webmaster said— 1 week later
HB said— 1 week later
sandepp said— 3 weeks later
Myfacefriends said— 1 month later
Gercek said— 1 month later
Joshua Heiland said— 1 month later
Rahul said— 1 month later
hermawan75 said— 2 months later
cancel bubble said— 2 months later
Adam Roessler said— 2 months later
Bony Yousuf said— 2 months later
vzagkid said— 3 months later
directory said— 3 months later
Hari Krishnan said— 5 months later
Mike O'Reilly said— 5 months later
Peter said— 6 months later
jignesh said— 6 months later
foxy said— 6 months later
Sriraj said— 7 months later
Dina said— 7 months later
MC said— 9 months later
Dustin Fisher said— 9 months later
bo said— 9 months later
Derek said— on May 29, 2009 later
jacob said— on May 29, 2009 later
satyam said— on May 29, 2009 later
Vipin Vijayan said— on May 29, 2009 later
Respond to this post—