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 />

• The successCallback function parameter tells the browser which function you<br />

want called when the location data is made available. This is important because<br />

operations such as fetching location data may take a long time to complete. No<br />

user wants the browser to be locked up while the location is retrieved, and no<br />

developer wants his program to pause indefinitely—especially because fetching<br />

the location data will often be waiting on a user to grant permission. The<br />

successCallback is where you will receive the actual location information and act<br />

on it.<br />

• However, as in most programming scenarios, it is good to plan for failure cases. It<br />

is quite possible that the request for location information may not complete for<br />

reasons beyond your control, and for those cases you will want to provide an<br />

errorCallback function that can present the user with an explanation, or perhaps<br />

make an attempt to try again. While optional, it is recommended that you provide<br />

one.<br />

• Finally, an options object can be provided to the <strong>HTML</strong>5 Geolocation service to<br />

fine-tune the way it gathers data. This is an optional parameter that we will<br />

examine later.<br />

Let’s say that you’ve created a JavaScript function on our page named updateLocation() in which<br />

you update the contents of the page with the new location data. Similarly, you’ve created a<br />

handleLocationError() function to handle the error cases. We’ll examine the details of those functions<br />

next, but that means that your core request to access the user’s position would be the code shown in the<br />

following example:<br />

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

The updateLocation() Function<br />

So, what happens in the updateLocation() call? It’s actually quite simple. As soon as the browser has<br />

access to the location information, it will call updateLocation() with a single parameter: a position<br />

object. The position will contain coordinates—as the attribute coords—and a timestamp for when the<br />

location data was gathered. While you may or may not need the timestamp, the coords attribute<br />

contains the crucial values for the location.<br />

The coordinates always have multiple attributes on them, but it is up to the browser and the<br />

hardware of the user’s device whether they will have meaningful values. The following are the first three<br />

attributes:<br />

• latitude<br />

• longitude<br />

• accuracy<br />

These attributes are guaranteed to have values and are fairly self-explanatory. latitude and<br />

longitude will contain the <strong>HTML</strong>5 Geolocation service’s best determined value of the user’s location<br />

specified in decimal degrees. accuracy will contain a value in meters which specifies how close the<br />

latitude and longitude values are to the actual location, with a 95% confidence level. Due to the nature of<br />

<strong>HTML</strong>5 Geolocation implementations, approximation will be common and coarse. Make sure to check<br />

the accuracy of the returned values before you present them with any certainty. Recommending a user<br />

to visit a “nearby” shoe store that is actually hours away could have unintended consequences.<br />

97

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

Saved successfully!

Ooh no, something went wrong!