Start your free trial now, and begin learning software, business and creative skills—anytime, anywhere—with video instruction from recognized industry experts.

Start Your Free Trial Now

Getting the current position

Getting the current position provides you with in-depth training on Developer. Taught by Andy Olsen … Show More

HTML5 Power Workshop

with Andy Olsen

Video: Getting the current position

Getting the current position provides you with in-depth training on Developer. Taught by Andy Olsen as part of the HTML5 Power Workshop
please wait ...
Getting the current position
Video Duration: 5m 55s 3h 9m Intermediate


Getting the current position provides you with in-depth training on Developer. Taught by Andy Olsen as part of the HTML5 Power Workshop

View Course Description

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
Developer Web video2brain

Getting the current position

In this lesson, we're going to look at the geolocation API for making a one-off request to get your current position. The example is in the file called getincurrentposition.html. This is in your Project Files folder. You just need to make sure that you copy it into a web server folder first before you run it. Now when I click the button, there's a geolocation API call in Javascript, and the browser intercepts the request and asks whether or not to proceed. Let's see what happens when I click the Deny button.

Now in that case, an error occurs, and I've got some error handling in the application which displays a suitable error message, the user denied geolocation. In order to actually get any further with the example, in Chrome there's a button on the address bar where you can change the settings. I can clear the settings. And then if I press F5, it'll forget the fact that I've denied access once, and then if I click the button again, I can get the chance to click the Allow button this time to actually proceed.

So in that case, we get back the latitude, longitude, and the location accuracy. And then potentially, dependent on which device mechanism was used, we might also get back the head in speed, altitude, altitude accuracy, and the time stamp. So, that's the example, let's see what the code looks like. I have the code opened in Komodo Edit and first of all, just skim to the bottom of the HTML to show you what the page looks like. So, on line 112 we have a div. This is the div that appears on the right hand side on the webpage, displaying all the geolocation information.

So, it's this div here that we're talking about. And the div has a series of text boxes. The first three text boxes represent the core information. So I've called them latitude, longitude and location accuracy. And then I have some additional text boxes here which display the optional information, if you like. So, that's the heading, the speed the altitude and the altitude accuracy and the time stamp. Now, on the web page, when I click the Get Current Position button, let me show you the code that gets invoked in that case.

It's near the top of the example, it's on line forty two. This is the click handler for the Get button. So the first thing you have to do is to determine, does your browser actually support geolocation? And this is how you make that test. Take the navigator object and test the geolocation property. And if you have that property then you do support geolocation. So, in that case I display a suitable method to the user saying that your browser does support geolocation, it's great. And this is how you can make a one-off request to actually get your current position.

You take your geolocation object and you call the get current position method. Now this method is asynchronous because getting geolocation information can be quite slow, you know it might take several seconds for a satellite signal to bounce back and forth. So this is an asynchronous call. You don't get back the information immediately. What you've got to do is to provide at least two callback functions. The first callback function will handle the real data if it succeeded, and the second callback is handling any errors that might have occurred.

So, let's take a look at the position callback function first of all. And that's down here on line 62. I just scroll it to end to see what to expect. So, your position callback function receives a position object. And the position object has a coords property that contains all the coordinate information about your location. So, the coords property has latitude and longitude, accuracy and head-in and so on. So, some of these might be null. Obviously, it depends on what your browser is capable of delivering. You also get back a time stamp telling you when the geolocation information was actually retrieved.

Now, on line 75, just to be on the safe side, I've got an extra check in here. Maybe the latitude or the longitude came back as null. It could happen. So, in that case, I display an error message or warning message to the user to say your browser does support geolocation, but on this particular occasion, we couldn't get it. So, the user might want to try again later on. Assuming that we have got the information and we display it all on the user interface as we've seen just now, when we ran the example.

So, that's the position callback function that displays the geolocation information. And then other thing we need to look at is the error callback. If anything goes wrong, this error callback will be invoked. It receives an error object and the error object has a code property and the code property was one, two or three, the error code will either be permission denied, position unavailable, or time out. So, permission denied is what we got because the user actually declined access.

Position unavailable would happen if you're in a dark place where you don't have any access to any geolocation information or timeout. A timeout can occur if the geolocation information wasn't retrieved in the appropriate time. There are options that you can specify to say how long you're prepared to wait for the geolocation information to come back. So, that concludes the example. I just summarized the key points. In order to test whether your browser supports geolocation, you can use this test here. In order to make a one-off request.

You can take your geolocation object, and call the get current position function. And you provide two callback functions to determine how to handle the information, or any errors that might occur.

There are currently no FAQs about HTML5 Power Workshop.






Don't show this message again
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.

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 Already a member? Log in

* Estimated file size

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


You have completed HTML5 Power Workshop.

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


Upgrade to View Courses Offline


With our new Desktop App, Annual Premium Members can download courses for Internet-free viewing.

Upgrade Now

After upgrading, download Desktop App Here.

Become a Member and Create Custom Playlists

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

Get started

Already a member?

Log in

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:

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.

You started this assessment previously and didn’t complete it.

You can pick up where you left off, or start over.

Resume Start over

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.

Sign up and receive emails about 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

Sign up and receive emails about 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.