Information Technology Reference
In-Depth Information
Where Am I Going?
To round out the capabilities we're exploring, we can ask ourselves—or more accurately
the device—
Where am I going?
There are two approaches that are worth covering. First,
our trusty geolocation data members include two more useful values,
position.coords.speed
and
position.coords.heading
. These indicate the last known
speed of the device, and the last known directional heading. These values sound great
for tasks like plotting movement on a map, and, in theory, that's exactly what they're
designed to provide. In practice, however, a disturbingly large number of devices either
don't report data for these values, or use such simplistic GPS chipsets as to not support
collecting it in the first place.
This brings us to an alternative approach. To date, we've been showing off the
getCurrentPosition()
method of
navigator.geolocation
. This method has a peer
method that is a little more sophisticated and useful when it comes to determining
location over time, and the movement that goes with it. This is the
watchPosition()
method.
The
watchPosition()
method, in its various overloaded forms, has fairly similar behavior
to our
getCurrentPosition()
method. It registers a callback function to use for normal
positioning, and another callback function for error handling, but it also accepts a range
of options, including a timer option for how frequently it should sleep and wake itself and
keep calling
your callbacks. The
watchPosition()
will continue to operate until halted by
a call to
clearWatch()
, or its parent process terminates. Listing 9-4 shows the next
iteration of our ongoing example application, from ch09-example05.html.
Listing 9-4.
Geolocation tracking over time(and movement!)
<html>
<head>
<title>Android Web Application Development-Geolocation example 5</title>
<script type="text/javascript">
function supportsGeo () {
if (navigator.geolocation) {
return true;
} else {
return false;
}
}
function changeDiv (name, data) {
var div = document.getElementById(name);
if(div)
{
div.innerHTML = data;
}
}
function startWatch () {
var geoData = "";
var watchOptions = { frequency: 5000 };
if (supportsGeo()) {