HTML5 Guidelines for Web Developers

(coco) #1
7.2 A First Experiment: Geolocation in the Browser 191

Figure 7.1 Mozilla Firefox asks for permission to share your location


Three arguments are passed to the function call:


z A function to be executed after the position has successfully been
determined (success callback)


z A function that can react to errors if the position could not be determined
(error callback)


z Value pairs influencing how the position is determined


According to the specification, the two latter arguments are optional; the success
callback always has to be specified. So as not to impede the JavaScript sequence,
getCurrentPosition() has to be executed asynchronously in the background,
and the relevant callback function can only be called once the position is known
or an error has occurred.


In this very short example, both callback functions are implemented as anony-
mous functions; errors are not considered. The value pair enableHighAccuracy:
true tells the browser to calculate the position as accurately as possible. On an
Android cell phone, this setting causes activation of the internal GPS sensor
(more on this in section 7.3, Technical Background of Determining Position).
Finally, maximumAge specifies the time in milliseconds during which an already
determined position can be reused. After that time, the position has to be rede-
termined—in our case, every ten minutes.


After successfully determining the position, the variable pos of the success call-
back in the so-called Position interface contains coordinate data (pos.coords)
plus a timestamp in milliseconds since 1970 (pos.timestamp). Figure 7.2 shows
the available attributes and their respective values, if present.

Free download pdf