HTML and CSS Reference
In-Depth Information
Property
Description
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.
maximumAge
Listing 1-5 shows the getCurrentPosition method in use, with all parameters specified.
LISTING 1-5 Using the getCurrentPosition method
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8"/>
<title></title>
<script>
window.onload = function () {
var geoLocator = window.navigator.geolocation;
var posOptions = {enableHighAccuracy: true,timeout: 45000};
geoLocator.getCurrentPosition(successPosition, errorPosition,
posOptions);
}
function successPosition(pos) {
alert(pos);
}
function errorPosition(err) {
alert(err);
}
</script>
</head>
<body>
<div id="geoResults">
<p>Current Location is:</p>
</div>
</body>
</html>
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