The coordinates property returns the position and altitude of the device on Earth.
Get the latitude and longitude of the user's position:
Click the button to get your coordinates.
<!DOCTYPE html> <html> <body> <button onclick="getLocation()">Test</button> <p id="demo"></p> <script> var x = document.getElementById("demo"); function getLocation() {/* ww w .ja v a2 s . com*/ if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition); } else { x.innerHTML = "Geolocation is not supported by this browser."; } } function showPosition(position) { x.innerHTML = "Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude; } </script> </body> </html>
Coordinates Properties
Property | Description |
---|---|
coordinates.latitude | latitude in decimal degrees |
coordinates.longitude | longitude in decimal degrees |
coordinates.altitude | altitude in meters, relative to sea level |
coordinates.accuracy | the accuracy of the latitude and longitude properties in meters |
coordinates.altitudeAccuracy | the accuracy of the altitude property in meters |
coordinates.heading | the direction in which the device is traveling specified in degrees 0 degrees represents true north east is 90 degrees west is 270 degrees. If speed is 0, heading is NaN. If the device is unable to provide heading information, this value is null |
coordinates.speed | the velocity of the device in meters per second. |