25.02.2013 Views

Peter Lubbers - Pro HTML 5 Programming

Pro HTML 5 Programming

Pro HTML 5 Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

104<br />

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

Writing the <strong>HTML</strong> Display<br />

Let’s start with the <strong>HTML</strong> display. We kept it quite simple for this exercise because the real interest is in<br />

the script driving the data. We display a simple table with rows for latitude, longitude, accuracy, and a<br />

timestamp in milliseconds since our last update. In addition, we’ll put a few status text indicators in<br />

place so that the user can see the summary of distance traveled (see Listing 4-8).<br />

Listing 4-8. Code for the Distance Tracker <strong>HTML</strong> page<br />

<strong>HTML</strong>5 Geolocation Distance Tracker<br />

<strong>HTML</strong>5 Geolocation is not supported in your browser.<br />

Current Position:<br />

<br />

<br />

Latitude<br />

?<br />

<br />

<br />

Longitude<br />

?<br />

<br />

<br />

Accuracy<br />

?<br />

<br />

<br />

Last Timestamp<br />

?<br />

<br />

<br />

Current distance traveled: 0.0 km<br />

Total distance traveled: 0.0 km<br />

These table values are all defaulted for now and are populated once data starts flowing into the<br />

application.<br />

<strong>Pro</strong>cessing the Geolocation Data<br />

Our first JavaScript code section should look familiar. We’ve set a handler—loadDemo()—that will<br />

execute as soon as the page completes loading. This script will detect if <strong>HTML</strong>5 Geolocation is supported<br />

in the browser and use a handy utility to change the status message on the page to indicate what it finds.<br />

It will then request a watch of the user’s position, as shown in Listing 4-9.

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

Saved successfully!

Ooh no, something went wrong!