navigator.geolocation property. The W3C provides a Geolocation API Specification that describes how to access geolocation data.
The Geolocation sample demonstrates how to access geolocation data. The code for this sample is located in <Amazon Apps & Games Services SDKs>/Web/Cookbook/Geolocation/. The Web App SDK can be downloaded from the SDK Download page.
Detect Whether Geolocation is Supported
To detect whether geolocation is supported, check to see whether thenavigator.geolocation property is present. If it is present, geolocation is supported.
Request Geolocation Data for the User’s Current Position
After you have verified that thenavigator.geolocation property is present, you can request geolocation data for the user’s current position by calling navigator.geolocation.getCurrentPosition() with a minimum of one and maximum of three arguments. The arguments, in order, are:
- (Required) A callback to a function that is called when the current location of the device is successfully determined.
- (Optional) A callback to a function that is called when an error occurs when attempting to determine the current location of the device.
- (Optional) An object encapsulating position-finding options.
- enableHighAccuracy (Boolean) provides a hint that the app would like to receive the best possible results. High-accuracy results might result in slower response times and/or increased power consumption. The user might deny this capability, or the device might not be able to provide results that are any more accurate than if this flag was set to false. The intent of this flag is to allow apps to inform the implementation that they do not require high-accuracy results so that the implementation can avoid using geolocation providers that consume a significant amount of power, such as GPS.
- timeout (long) is the maximum number of milliseconds that can elapse between the call to
getCurrentPosition()and when the corresponding success callback is invoked. Iftimeoutmilliseconds elapse and no other errors have occurred, the error callback is called with a timeout error code. - maximumAge (long) is the maximum number of milliseconds old a cached location can be and still be used. If
maximumAgeis set to zero, the implementation must immediately attempt to get new position data. SettingmaximumAgeto infinity means that cached data will always be returned, regardless of age.
getCurrentPosition() with a success callback, error callback, and options object defined. The implementation of the success callback is shown in the section called Using Geolocation Data. The implementation of the error callback is not shown.
Use the Geolocation Data
After you have requested geolocation data and the request is successfully processed, the success callback is called. The callback function receives a position object that contains a coordinates object and a timestamp. The coordinates object contains the following properties:- latitude specified in decimal degrees.
- longitude specified in decimal degrees.
- altitude is the height, in meters, of the position above the WGS84 ellipsoid. Might be null if the implementation cannot provide this information.
- accuracy of latitude and longitude, specified in meters with a 95% confidence level.
- altitudeAccuracy specified in meters with a 95% confidence level. Might be null if the implementation cannot provide altitude information.
- heading is the device’s direction of travel, specified in degrees such that 0° ≤ heading < 360°. Might be NaN if the device is stationary (i.e., speed is 0). Might be null if the implementation cannot provide heading information.
- speed is the magnitude of the horizontal component of the device’s current velocity, specified in meters per second. Might be null if the implementation cannot provide speed information.

