HTML and CSS Reference
In-Depth Information
Figure 12-2.
Finding the user's location using the
getCurrentPosition()
method
The web form consists of a button and a table. Clicking the Show Current Location button fills the
table with the location information. Note that some of the table cells contain undefined values, indicating
that those pieces of information are unavailable.
The jQuery code responsible for retrieving the location information is given in Listing 12-1.
Listing 12-1.
Retrieving the User's Location Information
$(document).ready(function () {
if (!Modernizr.geolocation) {
alert("This browser doesn't support the Geolocation API.");
return;
}
$("#btnShowCurrent").click(function () {
var options = {
enableHighAccuracy: false,
timeout: 5000,
maximumAge: 3000
};
window.navigator.geolocation.getCurrentPosition(OnSuccess, OnError, options);
});
});
This code shows the
ready()
function that checks whether the Geolocation API is supported in the
browser. This is done using the
geolocation
property of the
Modernizr
object. The code then wires an
event-handler function to the
click
event of the Show Current Location button.