HTML and CSS Reference
In-Depth Information
If this is set, the API is being told to use a cached result if available, rather
than make a new call to get the current position. The default is zero, so a
new call is always be made. If maximumAge is set to a value and the cache
isn't older than the allowable age, the cached copy is used. This value is
measured in milliseconds.
Listing 1-5 shows the getCurrentPosition method in use, with all parameters specified.
LISTING 1-5 Using the getCurrentPosition method
<html lang="en" xmlns="">
<meta charset="utf-8"/>
window.onload = function () {
var geoLocator = window.navigator.geolocation;
var posOptions = {enableHighAccuracy: true,timeout: 45000};
geoLocator.getCurrentPosition(successPosition, errorPosition,
function successPosition(pos) {
function errorPosition(err) {
<div id="geoResults">
<p>Current Location is:</p>
When the code runs in the browser, some interesting things can happen. First, browser
security starts; users are asked whether they want to allow this application to determine their
location. In Internet Explorer, the message looks like the image in Figure 1-53.
FIGURE 1-53 The security warning presented by Internet Explorer when accessing the Geolocation API
If the user chooses to allow the application to proceed, everything is great. Otherwise, the
method throws an exception.
For purposes of demonstrating the code, select Allow For This Site from the drop-down list
so that the page can proceed. It might take a few seconds, but the call returns and shows a
message box that a position object exists as passed to the success callback method.
Search WWH ::

Custom Search