The Geolocation API allows a web application to access a user’s geographical location (latitude, longitude, and more), with the user’s permission. It’s commonly used for maps, ride-hailing apps, weather apps, delivery tracking, and location-based services.
If you want to break into tech without spending years in school, this intensive software development training in Abuja is built for you.
navigator.geolocationhttps or localhost)navigator.geolocation.getCurrentPosition(success, error, options); if ("geolocation" in navigator) {
navigator.geolocation.getCurrentPosition(
(position) => {
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
console.log("Latitude:", latitude);
console.log("Longitude:", longitude);
},
(error) => {
console.error("Error getting location:", error.message);
}
);
} else {
console.log("Geolocation is not supported by this browser.");
} position Object Explainedposition.coords.latitude // Latitude
position.coords.longitude // Longitude
position.coords.accuracy // Accuracy in meters
position.coords.altitude // Altitude (if available)
position.coords.speed // Speed (if available)
position.timestamp // Time location was obtained
const options = {
enableHighAccuracy: true, // Uses GPS if available
timeout: 5000, // Wait max 5 seconds
maximumAge: 0 // Do not use cached position
};
navigator.geolocation.getCurrentPosition(
(position) => {
console.log(position.coords);
},
(error) => {
console.error(error.message);
},
options
); function handleError(error) {
switch (error.code) {
case error.PERMISSION_DENIED:
console.log("User denied the request for Geolocation.");
break;
case error.POSITION_UNAVAILABLE:
console.log("Location information is unavailable.");
break;
case error.TIMEOUT:
console.log("The request to get user location timed out.");
break;
default:
console.log("An unknown error occurred.");
}
}
const watchId = navigator.geolocation.watchPosition(
(position) => {
console.log("Updated location:", position.coords);
},
(error) => {
console.error(error.message);
}
);
// Stop watching later
navigator.geolocation.clearWatch(watchId); Use cases:
Codeflare offers a 3-month intensive software development training in Abuja, designed to equip students with job-ready coding skills and practical experience that employers look for.
function openInGoogleMaps(lat, lng) {
window.open(
`https://www.google.com/maps?q=${lat},${lng}`,
"_blank"
);
} The JavaScript Geolocation API lets you:
✔ Get the user’s current location
✔ Track movement in real time
✔ Build location-aware web apps
It’s simple to use, powerful, and must be handled responsibly.
Latest tech news and coding tips.
Here’s a comprehensive, clear differentiation between a Website and a Web App, from purpose all the…
Visual Studio Code (VS Code) is powerful out of the box, but its real strength…
1. What Is a Variable in JavaScript? A variable is a named container used to store data…
1. What Is a Queue? A Queue is a linear data structure that follows the principle: FIFO – First…
Angular is a full-featured frontend framework built by Google for creating large, maintainable, and high-performance web applications.…
What Is Responsive Web Design? Responsive Web Design (RWD) is an approach to building websites…