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

HTML5 Power Workshop
Watching:

Getting the current position


From:

HTML5 Power Workshop

with Andy Olsen

Video: 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.

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

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.

Share a link to this course
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.

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
Welcome to the redesigned course page.

We’ve moved some things around, and now you can



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.

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