if ("geolocation" in navigator) {
/* geolocation is available */
} else {
/* geolocation IS NOT available */
}Using the Geolocation API / notebook
Using the Geolocation API
Runnable notebook: every JavaScript, HTML and CSS example on this page executes in a sandboxed local frame. Nothing leaves the browser.
navigator.geolocation.getCurrentPosition((position) => {
doSomething(position.coords.latitude, position.coords.longitude);
});const watchID = navigator.geolocation.watchPosition((position) => {
doSomething(position.coords.latitude, position.coords.longitude);
});navigator.geolocation.clearWatch(watchID);function success(position) {
doSomething(position.coords.latitude, position.coords.longitude);
}
function error() {
alert("Sorry, no position available.");
}
const options = {
enableHighAccuracy: true,
maximumAge: 30000,
timeout: 27000,
};
const watchID = navigator.geolocation.watchPosition(success, error, options);function success(position) {
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
// Do something with your latitude and longitude
}function errorCallback(error) {
alert(`ERROR(${error.code}): ${error.message}`);
}