New Feature: Playlist Center! Pick a topic and let our playlists guide the way—like a learning mixtape.

Start learning with our library of video tutorials taught by experts. Get started

HTML5 Power Workshop
Illustration by

Watching the position continuously


From:

HTML5 Power Workshop

with Andy Olsen

Video: Watching the position continuously

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.

Watch this entire course now—plus get access to every course in the library. Each course includes high-quality videos taught by expert instructors.

Become a member
please wait ...
HTML5 Power Workshop
3h 9m Intermediate May 31, 2012

Viewers: in countries Watching now:

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.

Topics include:
  • Using the Communications API
  • Understanding geolocation
  • Getting and watching the current position
  • Using web workers and WebSockets
  • Implementing mobile web user interfaces
  • Managing data in a mobile web application
  • Working offline
  • Using Web SQL
  • Using drag-and-drop
Subjects:
Developer Web Web Design Web Development video2brain
Software:
HTML
Author:
Andy Olsen

Watching the position continuously

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.

 
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.
Upgrade now


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

join now Upgrade now

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed HTML5 Power Workshop.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Are you sure you want to delete this note?

No

Notes cannot be added for locked videos.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.