2 minutes to read
In this article
Obtaining a user's location can easily be done using the
W3C Geolocation API. This API is exposed through the
navigator.geolocation property in the browser. The browser will display a notification to the user the first time this API tries to get the users location, and ask permission to share this data. The geolocation class has the following static methods.
Attempts to obtain the users location. If successful it will trigger the callback function. This callback will receive a Position object that contains coordinates of the user and possibly additional information such as accuracy speed, heading, altitude.
Monitors the users position and triggers the callback if the user location information changes. This method will return a number which is the ID of this task. It can be used to clear/stop this task.
Clears/stops a watch position task with the specified ID.
This tells the browser that it would like to receive the best possible results. This may result in slower response times or increased power consumption. Default is false.
The maximum length of time (expressed in milliseconds) that is allowed to pass from the call until the corresponding successCallback is invoked. Default is Infinity.
Indicates that the application is willing to accept a cached position whose age is no greater than the specified time in milliseconds. Default is 0.
Contains the geographic coordinate information.
The time when the position object was acquired.
The latitude coordinate value.
The longitude coordinate value.
The altitude in meters above the WGS84 ellipsoid. This value is not always returned.
Accuracy level of the latitude and longitude coordinates in meters.
Accuracy of the altitude value in meters. This value is not always returned.
The direction in which the device is pointed in degrees counting clockwise where true north is 0 degrees. This value is not always returned.
The speed at which the device is travelling in meters per seconds. This value is not always returned.