Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 10. Location Awareness with the ... > Beginner Recipe: Determining Your Lo...

Beginner Recipe: Determining Your Location with a Simple getCurrentPosition

In this recipe, the page will use the getCurrentPosition method with a success callback function to determine your current location and display the properties of the position object returned. Use these steps and the code in Listing 10.1 to create this recipe:

1.
Create a blank HTML page with a div (called btnFindMe) and the Find Me button, which will call the findMe function when clicked.

2.
Add the findMe function in a set of script tags with the following code to check for the Geolocation API, and then call the getCurrentPosition method:

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(geoSuccess);
} else {
    document.getElementById('myLocation').innerHTML =
        "Geolocation API Not Supported";
}

3.
Add the geoSuccess function that will handle the successful callback from the getCurrentPosition request.

4.
Add a second div (called myLocation) to the HTML in which you will display the returned position information from the getCurrentPosition.


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial