HTML and CSS Reference
In-Depth Information
Got you: the success handler
If the user permits the browser to share his geolocation and
there's no other error, the success handler will be called, which
is the fi rst argument to
getCurrentPosition
and
watchPosition
.
The handler receives a
Position
object containing two properties:
coords
object (containing coordinate information) and a
timestamp
.
The coordinates object is where the interesting stuff is sitting.
There are really two grades of data in the position object. The first
grade is appearing in all the browsers with geolocation support:
Chrome 5, Firefox 3.5+, Mobile Safari & Safari 5, and Android 2.0:
•
readonly attribute double latitude
•
readonly attribute double longitude
•
readonly attribute double accuracy
Note that accuracy is the measurement of the latitude and longi-
tude accuracy in meters. You could use this to show a radius of
accuracy if you were mapping the user's position.
The second grade of data inside the coordinates object are
supported, but they currently don't have any values associated.
They will be null, 0, or
NaN
in all the browsers currently support-
ing native geolocation:
•
readonly attribute double altitude
•
readonly attribute double altitudeAccuracy
•
readonly attribute double heading
•
readonly attribute double speed
Using the coordinate data, you could easily map the user's cur-
rent position on something like a Google map:
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function
¬
(position) {
var coords = position.coords;
showMap(coords.latitude, coords.longitude,
¬
coords.accuracy);
});
}
In a lot of applications, it's likely that the user will be offered
a manual way to set his current position. If the geolocation
method is available, the site may offer the advanced functional-
ity, progressively enhancing the page or the whole experience.