Find web visitor’s location automatically with javascript and Google APIs

Brian Cray does a great job of explaining the details on his site - http://briancray.com

Basically, put the following into a block and you're good to go.

<div id="yourinfo"></div>
<script type="text/javascript" src="http://www.google.com/jsapi?key=YOUR-GOOGLE-MAPS-API-KEY"></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>Not Sure Where You Are.</p>';
}
</script>

Contact Me

Feel free to contact me.

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.