Start learning with our library of video tutorials taught by experts. Get started
Viewed by members. in countries. members currently watching.
In this course, part of his series of titles on HTML5 and CSS3, author and expert Andy Olsen looks at advanced topics like geolocation, mobile development, web sockets, Web SQL, and web workers. You'll also learn how to communicate between pages downloaded from different servers and how to use the new Ajax features in XMLHttpRequest Level 2. After completing this workshop, developers be well equipped to start utilizing the powerful features of HTML5.
HTML5 geo location has two ways of working. You can either make a one-off request, find your current position, or you can make a request to watch your position over a period of time. And get posted back information as you move around. So we're going to take a look at that latter mechanism here in this demo. The file is called watchingposition.html, and it's in your project files folder. Remember that you have to run it through a web server for it to work. So this is what happens. When I click the Watch Position button, it makes a call to a function called Watch Position on my geolocation object.
And that will continually call me back as my postion changes. Now display the position on the right hand side of our webpage here. Now, because I'm running this application on a desktop browser, and I'm sitting here in my office as opposed to walking around town. My location isn't actually changing. So we don't actually see in the location being updated here. But I ran this application earlier on my iPhone and as I was moving around the information was updated continuously as my position changed. So you can start watching your position and you will continually receive notifications as your location changes.
At some point you might want to stop getting those updates. So there's a Clear watch function that you can call. And that's what happens when I click this button. It stops the watch from giving me back any more post back information. Right. Let's take a look at the code for this. I have it open in the editor. When you click the button to start the watch, this is the function that gets called. The do watch function. And we first of all test whether the browser supports geolocation. And presumably it does.
What you then do is on your geolocation object, you call the watch position function. So basically, there are two functions on geolocation. You know how to call get current position to get a one off request. Or you call it watch position to monitor your changes over a period of time. The parameters for those two functions are the same. So, you have to pass in a position call back which will recieve the position information as it becomes avaiable and I'll call it continously now. Because we're watching the position change over time.
The second parameter is your error callback. So that's the same as before. And the third parameter, is a set of option parameters which indicate how you want the call to take place. Now, these parameters, you can patch them into both functions. Okay so I've passed them into the watch position function here particularly while. If I called the get currentl position function I could also a specify these parameters if I wanted to. So these parameters indicate a flag called enable high accuracy. So by default, that's false.
But if you set it to true, it gives a hint to the geolocation mechanism on your device, that tries hard as possible to get accurate information. So it might use one mechanism such as GPS, as opposed to another mechanism, such as WiFi access points. The second parameter, you can specify if you want to here, is the timeout. The timeout by default is infinity. If you want to, you can set a upper limit. Like here, I've said, if the information doesn't become available in five seconds, an error will occur. And in that case, in your error handler you might have some other mechanism to get the geolocation information.
For example, you might just say to the user It didn't come back in time. Do you want to enter your latitude and longitude manually? Maybe the user actually knows vaguely speaking where they are. And the other piece of information is the maximum age. And by default that's zero milliseconds. The maximum age specifies the amount of staleness that you're prepared to allow. So here I've said, if you've already retrieved geolocation in the last ten seconds, then that's fine. I will take that information, I don't need it to be anymore accurate than that.
So, the smaller number, you specify in here, more often it'll have to go and get geolocation information. So, call the watch position function, pass in the position call back and the error call back. Optionally, if you want to, you can pass in an object, like this, to fine-tune the operation call. So, once you called watched position, you will receive, over a period of time, successive calls to your position call back function. Now, the position callback function has nothing new. This is the same as you might expect.
Except it gets the position coordinate information, latitude, longitude and so on and the time stamp. And then displays that information on the screen for the user to see. The error callback is also the same as you might expect. It just displays the error on the information area of the screen, just to let the user know what went wrong. Okay, the last thing we need to think about is the clear watch method. So, let me take you back up to the code. You want to note this, when I set up the watch. The watch position function returns a value like (INAUDIBLE) the watch ID.
This is just simply a number, such as one, or two, or three. And because you might have multiple watches going on at the same time, I store that in a global variable. And then when you clear your watch, you have to use that global variable to say which particular watch you want to clear. So, in my DoClear function, which is called when I click the Clear button, check to see if there's a watch currently going. And if so, you call the clearWatch function on your geolocation object, and you specify the watch id as the parameter.
And then I've simply set the watch ID to null, so that we know that there isn't any watch running any more. So, that's the end of this lesson. We've seen how you can make continuous updates to your geolocation information by calling the watch position function. And how you can specify fine-tuning for the watch position calls like so. We've also seen how you can clear watch operations by calling the clear watch function like here.
There are currently no FAQs about HTML5 Power Workshop.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.