<p>
Your location is <span id="latitude">0.00</span>° latitude by
<span id="longitude">0.00</span>° longitude.
</p>
<button id="get-location">Get My Location</button>GeolocationCoordinates: longitude property / notebook
GeolocationCoordinates: longitude property
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
let button = document.getElementById("get-location");
let latText = document.getElementById("latitude");
let longText = document.getElementById("longitude");
button.addEventListener("click", () => {
navigator.geolocation.getCurrentPosition((position) => {
let lat = position.coords.latitude;
let long = position.coords.longitude;
latText.innerText = lat.toFixed(2);
longText.innerText = long.toFixed(2);
});
});