HTML and CSS Reference
In-Depth Information
This example uses jQuery for DOM manipulation, and enableHighAccuracy is set to true to get as ac-
curate a position as possible.
Watching the Position Change over Time
The geolocation API provides a second method called watchPosition that takes in the same parameters as
getCurrentPosition . It works like setInterval in that it returns an ID that can be used to clear the
watch at a later time with clearWatch .
If you want to walk around a bit, run the code in Listing 23-3 , which uses watchPosition to log the lat-
itude and longitude to a <div> on a mobile browser and see how the numbers change.
Listing 23-3: Watching the position change
<html lang="en">
<meta charset="UTF-8">
<title>Watching the position</title>
<script src='js/jquery.min.js'></script>
function logPosition(position) {
$("#logs").prepend(position.coords.latitude + "," +
position.coords.latitude + "<br/>");
function positionError(error) {
var watchID = navigator.geolocation.watchPosition(
enableHighAccuracy: true
setTimeout(function() {
<div id='logs'></div>
You can see that after 30 seconds the watch is cleared to prevent the system from continuing to update the
Search WWH ::

Custom Search