Location Staycation

by fightingmonk on February 4, 2010

I’m playing with some mobile geo-location services and needed to test out the W3C Geolocation API. This supposedly works in Firefox 3.5 and Mobile Safari. Do let me know if you see something interesting in another browser.

This page is a work in progress…

Results

Your current location reports as: loading…;
OpenStreetMap translates that to: loading….


And the Codez…

<p id='loc_p'>Your current location reports as:
<em id='loc'>loading...</em>;<br />
OpenStreetMap translates that to: <em id='addr'>loading...</em>.</p>

<script language='javascript'>
var locWatchId = false;
window.onload=function() {
  if (navigator.geolocation) {
    locWatchId = navigator.geolocation.watchPosition(updateLocation);
  }
  else {
    document.getElementById('loc_p').innerText =
      "Sorry, your browser doesn't appear to support location services.";
  }
}

function updateLocation(position) {
  var text = '@['+position.coords.latitude+', '+
      position.coords.longitude+']';
  document.getElementById('loc').innerText = text;
  var revGeocodeUrl =
        "http://nominatim.openstreetmap.org/reverse?lat=" +
        position.coords.latitude + "&lng=" +
        position.coords.longitude +
        "&zoom=18&format=json&json_callback=showAddr";

  var scriptNode = document.createElement('script');
  scriptNode.type = 'text/javascript';
  scriptNode.src = revGeocodeUrl;
  document.getElementsByTagName('head')[0].appendChild(scriptNode);
}

function showAddr(addrData) {
  if ( addrData && addrData.display_name ) {
    document.getElementById('addr').innerText = addrData.display_name;
  }
  else {
    document.getElementById('addr').innerText = 'unknown';
  }
}
</script>
  • hemp

    Doesn't work in the Chromes..

Previous post:

Next post: