Start learning with our library of video tutorials taught by experts. Get started
Viewed by members. in countries. members currently watching.
In HTML5 First Look, author James Williamson introduces the newest HTML specification, providing a high-level overview of HTML5 in its current state, how it differs from HTML 4, the current level of support in various browsers and mobile devices, and how the specification might evolve in the future. Exercise files accompany the course.
One of the most confusing things about HTML5 is deciphering what is and what is not HTML5. There are multiple associated technologies and APIs that have at various times either been a part of the HTML5 specification or have been labeled as being HTML5 for lack of a better buzzword. In this chapter, we are going to discuss some of the technologies associated with HTML5 and I want to start with the Geolocation API. The Geolocation API allows you to build applications that can determine where the user is and then share or act on that information.
The specification is currently in the working draft stage. Although you can view the latest editor's draft on the W3's site. While in the past, many web applications have used IP addresses to determine location, the Geolocation API leaves the method of discovery up to the user agent and gives authors a set of methods and objects to handle and process location data. This approach is much more flexible. It has a higher degree of accuracy and allows authors to take advantage of the many new powerful location-aware mobile devices.
Unlike using IP addresses for location, the Geolocation API is opt-in, meaning the user has to give permission to the application in order for the location data to be received. This addresses privacy concerns around the use of Geolocation and needs to be accounted for in the planning and design of any location-aware application. Currently, the Geolocation API is supported by Chrome versions 5 and above, Firefox versions 3.5 and above, Safari 5.0 and above, and on recent iPhone and Android operating systems.
While support is increasing, you will have to use workarounds if you want your application to work in non- supported devices or browsers. We are going to talk more about those in just a moment. Now that we know what the Geolocation API is, let's take a closer look at how it works. The core of the Geolocation API is built around the global navigator object. Using the navigator object, you can use two methods. getCurrentPosition and watchPosition to retrieve location data. The getCurrentPosition method is a one-shot method that retrieves a single set of data.
The watchPosition method creates a watch that allows applications to track location data over a period of time. Here's a sample function call for location data. As you can see, this uses a pretty simple form of Geolocation detection. First, we check to see if the Geolocation object exists. If it does, then we call for the data. If it doesn't, we could use one of our workarounds like Google Gears or simply inform the user that this device doesn't support Geolocation. You could also use a more robust detection method, such as the Modernizr library that we discussed earlier.
The call itself returns two functions that we then must define. plotLocation is the name I have given to the handler that is called if the method is successful. The second handler, plotError, is called if the location is not received. Now this is an optional handler. It's not required for getCurrentPosition to work. It's worth mentioning here that the Geolocation API has extensive error handling and allows you to respond to errors at any stage of the application. Okay, so let's say your call was successful. Well, now what? Well the callback function is going to contain an object with two properties.
The coords property and timestamp. As you can guess, the timestamp tells you when the data was received, which allows your applications to react out current position data is. The coords object contains a lot more information. It handles latitude, longitude, altitude accuracy, altitudeAccuracy, heading and speed. If a device is unable to supply any of these properties, a null value is going to be returned for them. So now that we've got that down, let's take a look at a simple callback function.
Here we've received the position object and retrieved the latitude and longitude coordinates from the coord object. Those could then be passed to a Google Maps for example, as a request for a new or updated map. There are of course many additional methods and objects available through the Geolocation API. So be sure to dig into the specification for more information and some sample code. Before we wrap up, I want to mention a couple of important considerations for developing apps with Geolocation. First, be aware of the opt-in nature of the Geolocation API.
The user acceptance interface is device-dependent and will change based on where your app is accessed. Also, the opt-in notifications tend to be what we call "blocking," meaning the user must accept in order for the application to continue. Making sure that you inform your user very early in the application process that they must agree to share their location will make for a much more successful application. Finally, the current uneven implementation of the Geolocation API means that you're going to need to detect for its support and provide fallback content and perhaps an alternate way of providing location data.
I think you'll agree that while not strictly HTML5, the Geolocation API is going to allow you to create some very interesting next-generation web applications.
There are currently no FAQs about HTML5 First Look.
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.