<geolocation autolocate>
<button id="fallback">Use location</button>
</geolocation>HTMLGeolocationElement / notebook
HTMLGeolocationElement
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
<p id="status">Status:</p><div id="map"></div>const statusElem = document.querySelector("#status");if (typeof HTMLGeolocationElement === "function") {
// <geolocation> is supported
} else {
// <geolocation> is not supported; use fallback button
}const geo = document.querySelector("geolocation");geo.addEventListener("location", () => {
if (geo.position) {
console.log(
`${geo.position.coords.latitude},${geo.position.coords.longitude}`,
);
drawMap(geo.position.coords.latitude, geo.position.coords.longitude, geo);
} else if (geo.error) {
console.log(geo.error.message);
}
});geo.addEventListener("promptdismiss", notifyUserRetrySelection);
geo.addEventListener("promptaction", notifyUserGrantPermission);function notifyUserRetrySelection() {
statusElem.textContent =
'Please press the "Use location" button again and allow location for this site.';
}
function notifyUserGrantPermission() {
if (geo.permissionStatus === "denied" || geo.permissionStatus === "prompt") {
statusElem.textContent =
'Please press the "Use location" button again and allow location for this site.';
}
}const fallback = document.querySelector("#fallback");fallback.addEventListener("click", () => {
navigator.geolocation.getCurrentPosition(
(position) => {
drawMap(position.coords.latitude, position.coords.longitude, fallback);
},
(error) => {
statusElem.textContent += `${error.message}, `;
},
);
});function drawMap(lat, long, btn) {
const map = L.map("map").setView([lat, long], 13);
L.tileLayer("https://tile.openstreetmap.org/{z}/{x}/{y}.png", {
maxZoom: 19,
attribution:
'© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
}).addTo(map);
const marker = L.marker([lat, long]).addTo(map);
statusElem.textContent = "Map drawn successfully.";
btn.style.display = "none";
}