Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a couple of examples in JavaScript and JSON (with the help of <a href="http://jquery.com/">jQuery</a>) using both the IP lookup method (with the help of <a href="http://ipinfodb.com/ip_location_api_json.php">IPinfoDB</a>) and the <a href="http://diveintohtml5.info/geolocation.html">Geolocation API</a> method (with the help of <a href="http://code.google.com/apis/maps/documentation/javascript/">Google Maps API</a> and <a href="http://developer.yahoo.com/yql/console/">YQL</a>).</p> <p>In both examples I'm retrieving the region and country but there are several values you can choose from. Be aware these examples don't do any error handling and here they are edited for brevity, otherwise see <a href="http://jsfiddle.net/Marcel/ZjXXh/embedded/result/">the full demo</a>.</p> <h2>IP Lookup in JavaScript</h2> <p>This method is good because it's easy to implement and is fairly accurate at a country level but accuracy drops considerably for anything more specific.</p> <pre><code>// API key excluded for brevity, see full demo. var apiurl = 'http://api.ipinfodb.com/v3/ip-city'; $.getJSON(apiurl+'/format=json&amp;callback=?', function(data){ $("h3#location").html(data.regionName + ", " + data.countryName); } ); </code></pre> <h2>Geolocation API</h2> <p>Although by no means perfect, this method is as accurate as it gets. User is prompted to share geolocation details which may reduce usage.</p> <pre><code>navigator.geolocation.getCurrentPosition(function(pos){ $.getJSON(apiurl+'/format=json&amp;callback=?', function(data){ var regionName = data[...]AdministrativeAreaName; var countryName = data[...]CountryName; $("h3#location").html(regionName + ", " + countryName); } ); }); </code></pre> <p>In there you'll see I used the <a href="http://code.google.com/apis/maps/documentation/javascript/basics.html">Google Maps API v3</a> by way of <a href="http://developer.yahoo.com/yql/console/">YQL</a> so that a JSON callback is possible.</p> <h2>Full demo: <a href="http://jsfiddle.net/Marcel/ZjXXh/embedded/result/">http://jsfiddle.net/ZjXXh</a></h2>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload