Lesson 1: Basic positioning CHAPTER 14 541
■■timestamp Gets the time that the Coordinates object was created. The format for
the timestamp is milliseconds since the start of the Unix Epoch and can be converted
to a regular date and time by using the following code.
var dateTime = new Date(timeStamp).toLocaleString();
By focusing on the coords property that gets a Coordinates object, the Coordinates object
contains the following information about the current location.
■■latitude Gets the latitude in decimal degrees
■■longitude Gets the longitude in decimal degrees
■■altitude Gets the height in meters
■■accuracy Gets the accuracy of the coordinates in meters
■■altitudeAccuracy Gets the accuracy of the altitude in meters
■■heading Gets the direction of travel in degrees
■■speed Gets the speed of travel in meters/second
The device hardware is the determining factor regarding the properties returned. For
example, with a GPS, most properties are populated, but if location is determined by using
your IP address, you might be missing altitude, heading, and speed-related information.
Retrieving the current position
It is easy to retrieve the current position by using the Geolocation object’s getCurrentPosition
method. At a minimum, you need to pass a callback method to receive a Position object,
which has coords and timestamp properties. For the subsequent examples, consider the
following webpage.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<link href="GeoLocation.css" rel="stylesheet" />
<script src="Scripts/jquery-1.8.3.js"></script>
<script src="Scripts/GeoLocation.js"></script>
</head>
<body>
<div id="message">
</div>
</body>
</html>
The webpage has a reference to the Geolocation.css style sheet that is currently empty, the
jQuery library, and the Geolocation.js JavaScript file, which is currently empty. The <body>
element contains only a <div> element whose id is message.
The following code example demonstrates a simple call to the getCurrentPosition method.
/// <reference path="jquery-1.8.3.js" />