HTML and CSS Reference
In-Depth Information
Table 12-3.
Properties of the
position
Object
Property
Description
latitude
Returns the latitude value in decimal format.
longitude
Returns the longitude value in decimal format.
accuracy
Contains a distance in meters that specifies how close the latitude and longitude
values are to the actual location. The value of the
accuracy
property increases as the
accuracy of the location data decreases.
altitude
Returns the height of the user's location in meters above the ellipsoid.
altitudeAccuracy
Provides the accuracy of the altitude value in meters.
heading
Indicates the direction of travel in degrees relative to north.
speed
Gives the ground speed in meters per second.
timestamp
Gives the timestamp at which the location data was returned. It's returned as
number of milliseconds since 1 January 1970. You can convert this value into a
JavaScript
Date
object for further use.
Not all the properties discussed in Table 12-3 are available on all the devices. For example, a desktop
computer doesn't supply properties such as
speed
and
heading
. The four properties
latitude
,
longitude
,
accuracy
, and
timestamp
are available on all devices; the other properties aren't guaranteed to be
supported. If no data is available for a property,
null
is returned.
The code in Listing 12-2 reads various properties and appends the values in a HTML table (
tblInfo
). If
an error occurs while finding the user location, the
OnError()
function is called.
OnError()
is shown in
Listing 12-3.
Listing 12-3.
OnError()
Function to Display Error Information
function OnError(err) {
alert(err.code + " : " + err.message);
}
The
OnError()
function is quite straightforward. The
err
object passed to the function has two
properties that give more information about the error:
code
and
message
. The possible error codes are
1
(
PERMISSION_DENIED
),
2
(
POSITION_UNAVAILABLE
), and
3
(
TIMEOUT
). The
message
property gives a descriptive
error message. Figure 12-3 shows an error message when permission to use the Geolocation API is denied.
Figure 12-3.
Error message after permission to use the Geolocation API is denied
Figure 12-3 shows the
code
and
message
properties in an alert box. You can, of course, check the
code
property and display a friendlier message for each error code.