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