Javascript examples for Browser Object Model:Navigator
The geolocation property returns a Geolocation object to locate the user's position.
The position is not available unless the user approves it.
This property is read-only.
A reference to the Geolocation object
The following code shows how to get the latitude and longitude of the user's position:
<!DOCTYPE html> <html> <body> <button onclick="getLocation()">Test</button> <p id="demo"></p> <script> var x = document.getElementById("demo"); function getLocation() {/*www . j ava 2 s . c o m*/ 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>