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.

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

• POSITION_UNAVAILABLE (error code 2)—The technique used to determine the user’s<br />

location was attempted, but failed.<br />

• TIMEOUT (error code 3)—A timeout value was set as an option, and the attempt to<br />

determine the location exceeded that limit.<br />

In these cases, you will probably want to let the user know that something went wrong. You may<br />

want to retry getting the values in the case of an unavailable or timed-out request.<br />

Listing 4-4 shows an example of an error handler in action.<br />

Listing 4-4. Using an error handler<br />

function handleLocationError(error) {<br />

switch(error.code){<br />

case 0:<br />

updateStatus("There was an error while retrieving your location: " +<br />

error.message);<br />

break;<br />

case 1:<br />

updateStatus("The user prevented this page from retrieving a location.");<br />

break;<br />

case 2:<br />

updateStatus("The browser was unable to determine your location: " +<br />

error.message);<br />

break;<br />

case 3:<br />

updateStatus("The browser timed out before retrieving the location.");<br />

break;<br />

}<br />

}<br />

The error codes are accessed from the code attribute of the provided error object, while the message<br />

attribute will give access to a more detailed description of what went wrong. In all cases, we call our own<br />

routine to update the status of the page with the necessary info.<br />

Optional Geolocation Request Attributes<br />

With both the normal and error cases handled, you should turn your attention to the three optional<br />

attributes that you can pass to the <strong>HTML</strong>5 Geolocation service in order to fine-tune how it gathers its<br />

data. Note that these three attributes can be passed using shorthand object notation, making it trivial to<br />

add them to your <strong>HTML</strong>5 Geolocation request calls.<br />

• enableHighAccuracy—This is a hint to the browser that, if available, you would like<br />

the <strong>HTML</strong>5 Geolocation service to use a higher accuracy-detection mode. This<br />

defaults to false, but when turned on, it may not cause any difference, or it may<br />

cause the machine to take more time or power to determine location. Use with<br />

caution.<br />

99

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

Saved successfully!

Ooh no, something went wrong!