Game Development Reference
In-Depth Information
case positionError.TIMEOUT:
doOnTimeout(positionError.message);
break;
}
}
// Ask the user if you may use Geolocation
navigator.geolocation.getCurrentPosition(getGeo,
doNoGeo);
The first part of the API involves asking the user for permission to get his or her loc-
ation. This is done by calling the
getCurrentPosition
function on the
geoloca-
tion
attribute of the global navigator object. The function takes two arguments,
namely a callback function to be called if the user allows the browser to share the
user's location and a callback function to be called if the user denies the application's
request.
If the user accepts the request from the application to share the geolocation, the call-
back is invoked with a
Geoposition
object passed in to it. This object has
nine
properties that we can use:
•
timestamp
: When the callback function was invoked
•
coords
: An instance of class
Coordinates
•
accuracy
: How accurate the GPS coordinate is (in meters)
•
altitude
: In meters
•
altitudeAccuracy
: How accurate the altitude is (in meters)
•
heading
: In degrees clockwise from north
•
latitude
: As a double
•
longitude
: As a double
•
speed
: In meters per second
There are only three attributes in the position object that are required to be present.
These are the
latitude
and
longitude
values, along with the
accuracy
attrib-
ute. All other values are optional and will be available if the hardware in use supports
them. Keep in mind, also, that this feature is equally available on mobile devices, so
it is possible and likely that the user's position changes somewhat during the course
of the application's usage. Thankfully, once the user has agreed to have his or her