HTML 5 Geolocation

In HTML 5 you can use the getCurrentPosition() method to retrieve the latitude and longitude of the user's current location. In this example we display the latitude and longtitude in the paragraph with id 'coords'.

Code


<p id="coords"></p>

<script>


function getCurrentLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(displayCoords);
} else {
document.getElementById("coords").innerHTML = "Geolocation is not supported by this browser.";
}
}

function displayCoords(position) {
document.getElementById("coords").innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
</script>

<script>
getCurrentLocation()
</script>