HTML and CSS Reference
In-Depth Information
option is to have the browser tell you when there's an updated location.
For this, the Geolocation API provides the watchPosition() method.
This screenshot shows my progress
through North London over a couple
of hours one afternoon. All I had to do
to get this information was open the
page in the phone's browser and then
keep the phone in my pocket. When
new geolocation information was
available, the browser activated the
callback function.
The code is almost identical to the previous example. the only
change is in the method called.
watchposition instead of
getCurrentPosition .
navigator.geolocation.watchPosition(
function (position) {
document.getElementById('location').innerHTML =
'Latitude: ' +
position.coords.latitude +
' Longitude: ' +
position.coords.longitude +
' with an accuracy of: ' +
position.coords.accuracy + 'm'
})
This function will
now be called
multiple times.
These lines are
unchanged.
Practical uses for geolocation
Now that you've seen the basics of acquiring position information, let's
consider how you might use the Geolocation API in practice. We'll look
at two simple examples: calculating how far the user is from a given
point, and showing the user on a map.
Search WWH ::




Custom Search