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.

}<br />

updateGeolocationStatus("Location updated at " + timestamp);<br />

// Schedule a message to send my location via WebSocket<br />

var toSend = ";" + document.getElementById("racerName").value<br />

+ ";" + latitude + ";" + longitude;<br />

setTimeout("sendMyLocation('" + toSend + "')", 1000);<br />

function sendMyLocation(newLocation) {<br />

if (socket) {<br />

socket.send(newLocation);<br />

updateSocketStatus("Sent: " + newLocation);<br />

}<br />

}<br />

CHAPTER 9 ■ USING THE <strong>HTML</strong>5 WEB STORAGE API<br />

function startSendingLocation() {<br />

var geolocation;<br />

if(navigator.geolocation) {<br />

geolocation = navigator.geolocation;<br />

updateGeolocationStatus("<strong>HTML</strong>5 Geolocation is supported in your browser.");<br />

}<br />

else {<br />

geolocation = google.gears.factory.create('beta.geolocation');<br />

updateGeolocationStatus("Geolocation is supported via Google Gears");<br />

}<br />

}<br />

<br />

<br />

<br />

// register for position updates using the Geolocation API<br />

geolocation.watchPosition(updateLocation,<br />

handleLocationError,<br />

{maximumAge:20000});<br />

We won’t spend too much space covering this file in detail, as it is nearly identical to the tracker<br />

example in Chapter 6. The primary difference is that this file contains a text field for entering the racer’s<br />

name:<br />

Racer name: <br />

The racer’s name is now sent to the broadcast server as part of the data string:<br />

var toSend = ";" + document.getElementById("racerName").value<br />

+ ";" + latitude + ";" + longitude;<br />

To try it out for yourself, open two windows in a browser that supports <strong>HTML</strong>5 Web Storage,<br />

Geolocation, and WebSocket, such as Google Chrome. In the first, load the running club’s index.html<br />

page. You will see it connect to the race broadcast site using WebSocket and then await any racer data<br />

233

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

Saved successfully!

Ooh no, something went wrong!