Clean âĸ Professional
The Geolocation API lets websites access the userâs location (with permission). This is widely used in maps, food delivery apps, ride-booking, and weather updates.
Key Features:
The API is available through the navigator.geolocation object in JavaScript.
It mainly has three methods:
getCurrentPosition() â Get the current location once.watchPosition() â Track location continuously in real time.clearWatch() â Stop tracking location.Hereâs a simple example of getting the current position:
<button onclick="getLocation()">Get My Location</button>
<p id="output"></p>
<script>
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, showError);
} else {
document.getElementById("output").innerText = "Geolocation is not supported in this browser.";
}
}
function showPosition(position) {
document.getElementById("output").innerHTML =
"Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
function showError(error) {
switch(error.code) {
case error.PERMISSION_DENIED:
alert("User denied location request.");
break;
case error.POSITION_UNAVAILABLE:
alert("Location information unavailable.");
break;
case error.TIMEOUT:
alert("Location request timed out.");
break;
default:
alert("An unknown error occurred.");
}
}
</script>If you want to continuously monitor location (like in ride-tracking apps), use watchPosition().
let watchId = navigator.geolocation.watchPosition(pos => {
console.log("Latitude:", pos.coords.latitude,
"Longitude:", pos.coords.longitude);
});Errors can happen if:
Thatâs why error callbacks are important (as shown in the example above).
Supported by most modern browsers (Chrome, Edge, Firefox, Safari).
Requires HTTPS (except on localhost).
Accuracy depends on the device â GPS is more accurate than Wi-Fi or IP.
The built-in Geolocation API is simple and free, but limited.
For advanced features, developers often use:
HTML5 Geolocation API Example â Get & Track User Location
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML Geolocation API Example</title>
</head>
<body>
<h2>đ HTML Geolocation API Demo</h2>
<button onclick="getLocation()">Get My Location</button>
<button onclick="startTracking()">Start Tracking</button>
<button onclick="stopTracking()">Stop Tracking</button>
<p id="output"></p>
<script>
let watchId;
// 1. Get current location once
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, showError);
} else {
document.getElementById("output").innerText =
"â Geolocation is not supported in this browser.";
}
}
// Show latitude & longitude
function showPosition(position) {
document.getElementById("output").innerHTML =
"â
Current Location:<br>" +
"Latitude: " + position.coords.latitude + "<br>" +
"Longitude: " + position.coords.longitude;
}
// Handle errors
function showError(error) {
switch (error.code) {
case error.PERMISSION_DENIED:
alert("â User denied the location request.");
break;
case error.POSITION_UNAVAILABLE:
alert("â Location info unavailable.");
break;
case error.TIMEOUT:
alert("âŗ Location request timed out.");
break;
default:
alert("â ī¸ An unknown error occurred.");
}
}
// 2. Track location continuously
function startTracking() {
if (navigator.geolocation) {
watchId = navigator.geolocation.watchPosition((pos) => {
document.getElementById("output").innerHTML =
"đ Tracking...<br>" +
"Latitude: " + pos.coords.latitude + "<br>" +
"Longitude: " + pos.coords.longitude;
}, showError);
}
}
// 3. Stop tracking
function stopTracking() {
if (watchId) {
navigator.geolocation.clearWatch(watchId);
document.getElementById("output").innerText =
"âšī¸ Tracking stopped.";
}
}
</script>
</body>
</html>
