Usage: we can take advantage of the navigator.geolocation object to identify where the page is being accessed from. Note that the visitor must specifically grant permission for this to happen.
JavaScript overview: We should test for the navigator.geolocation object before proceeding. This can be as simple as if (navigator.geolocation)…
navigator.geolocation.getCurrentPosition(showLocation,mapError,{timeout:5000}); is the sort of initial action we would take if the object is present. Note there is a call to a showLocation function, a mapError function and a timeout parameter (in milliseconds). In the showLocation function we would interact with a unique identifier on the page (and update the innerHTML property, for example). In the mapError function, we might pass the error codes and some explanation (typical error codes range from 0 – 3). Examine the souurce code in the linked file for more details.
Example page (opens in a new tab/ window – view source code):
- http://learning-html5.info/examples/ExampleGeo1.html (this example page comes from a lynda.com tutorial and is slightly modified for this example).
References: