25.02.2013 Views

Peter Lubbers - Pro HTML 5 Programming

Pro HTML 5 Programming

Pro HTML 5 Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

100<br />

CHAPTER 4 ■ USING THE <strong>HTML</strong>5 GEOLOCATION API<br />

■ Note Curiously, the high accuracy setting is only a toggle switch: true or false. The API was not<br />

created to allow the accuracy to be set to various values or a numeric range. Perhaps this will be<br />

addressed in future versions of the specification.<br />

• timeout—This optional value, provided in milliseconds, tells the browser the<br />

maximum amount of time it is allowed to calculate the current location. If the<br />

calculation does not complete in this amount of time, the error handler is called<br />

instead. This defaults the value of Infinity, or no limit.<br />

• maximumAge—This value indicates how old a location value can be before the<br />

browser must attempt to recalculate. Again, it is a value in milliseconds. This value<br />

defaults to zero, meaning that the browser must attempt to recalculate a value<br />

immediately.<br />

■ Note You might be wondering what the difference is between the timeout and maximumAge options.<br />

Although similarly named, they do have distinct uses. The timeout value deals with the duration needed<br />

to calculate the location value, while maximumAge refers to the frequency of the location calculation. If<br />

any single calculation takes longer than the timeout value, an error is triggered. However, if the browser<br />

does not have an up-to-date location value that is younger than maximumAge, it must refetch another<br />

value. Special values apply here: setting the maximumAge to “0” requires the value to always be refetched,<br />

while setting it to Infinity means it should never be refetched.<br />

However, note that the API does not allow us to tell the browser how often to recalculate the<br />

position. This is left entirely up to the browser implementation. All we can do is tell the browser what the<br />

maximumAge is of the value it returns. The actual frequency is a detail we cannot control.<br />

Let’s update our location request to include an optional parameter using shorthand notation, as<br />

shown in the following example:<br />

navigator.geolocation.getCurrentPosition(updateLocation,handleLocationError,<br />

{timeout:10000});<br />

This new call tells <strong>HTML</strong>5 Geolocation that any request for location that takes longer than 10<br />

seconds (10000 milliseconds) should trigger an error, in which case the handleLocationError function<br />

will be called with the TIMEOUT error code.<br />

Repeated Position Updates<br />

Sometimes once is not enough. Thankfully, the designers of the Geolocation service made it trivial to<br />

switch from an application that requests a user location one time to one that requests the location at<br />

regular intervals. In fact, it’s largely as trivial as switching the request call, as shown in the following<br />

examples:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!