Training Guide: Programming in HTML5 with JavaScript and CSS3 Ebook

(Nora) #1

546 CHAPTER 14 Making your HTML location-aware


Lesson review


Answer the following questions to test your knowledge of the information in this lesson. You
can find the answers to these questions and explanations of why each answer choice is correct
or incorrect in the “Answers” section at the end of this chapter.


  1. Which method gets your current location once from the Geolocation object?
    A. watchPosition()
    B. watchLocation()
    C. getCurrentPosition()
    D. getCurrentLocation()

  2. Which of the following is not a valid parameter on the getCurrentPosition method?
    A. Error callback function
    B. Position options object
    C. Success callback function
    D. Use GPS only


Lesson 2: Monitored positioning


If you were writing an application that plotted your current location on a map, it would be
more efficient to let the Geolocation API tell you when the location changes than to write
code that continues to poll for the current location. That’s the focus of this lesson.

After this lesson, you will be able to:
■■Use the Geolocation object to watch the current position.
■■Calculate the distance between two sample points.

Estimated lesson time: 20 minutes

Where are you now? How about now?


You can use the Geolocation object’s watchPosition method to retrieve continuous position
updates. This method takes the same parameters as the getCurrentPosition method. The dif-
ference is that when you call the watchPosition once, it continues calling the success function
until you call the clearWatch method to stop monitoring your position.
The watchPosition method returns an id, which is passed to the clearWatch method to end
the monitoring. In this example, the webpage is modified with the addition of a button to
start location monitoring and another button to end location monitoring, as follows.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
Free download pdf