2

Google analytics is able to find a visitors location using a Javascript library. How can this be accomplished with Javascript?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user1530249
  • 1,047
  • 3
  • 19
  • 28
  • 1
    http://stackoverflow.com/questions/3489460/how-to-get-visitors-location-i-e-country-using-javascript-geolocation – The Alpha Dec 26 '12 at 23:16
  • [Also this one](http://www.opal-creations.co.uk/blog/free-scripts-and-code/get-a-visitors-location-with-javascript). – The Alpha Dec 26 '12 at 23:18

2 Answers2

2

The navigator.geolocation object is how you ask a user agent for their location. Depending on the UA's settings, this may or may not prompt the user to allow/deny sending the data. Also, the geolocation data itself may be very variable in its precision (they give you a margin or error, though, so you can factor that in).

if (navigator.geolocation) {
    navigator.geolocation.getPosition(
        successFunction,
        failureFunction
    );
} else {
    noGeolocationFunction();
};

There's also a watchPosition method. Both are asynchronous, so you pass it success/failure functions to handle the returned object.

Semicolon
  • 6,793
  • 2
  • 30
  • 38
  • Note: This method will require the visitors permission in order to be obtained. You can try using this method [here](http://www.w3schools.com/html/html5_geolocation.asp). – Micah Dec 26 '12 at 23:38
  • True, as noted in the second sentence of the answer. But it has the advantage of high precision in most cases -- IP based sniffing cannot be relied on if you require anything more specific than a country, especially outside of the US. The question doesn't state how specific they need to get, but if, for example, they wanted to use the location to provide directions, the IP lookup is not adequate. – Semicolon Dec 26 '12 at 23:47
1

Google has an API for querying a visitors location. Find web visitor's location automatically with javascript and Google APIs

<!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=[apikey]"></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>
Chance
  • 11,043
  • 8
  • 61
  • 84
Micah
  • 1,221
  • 17
  • 42