📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials HTML Fundamentals Geolocation API

Geolocation API

4 min read
navigator.geolocation.getCurrentPosition(success, error) requests the user's location. Works on HTTPS only. The success callback provides coords.latitude and coords.longitude. Always handle the permission denied error.

Geolocation API

if ("geolocation" in navigator) {
    navigator.geolocation.getCurrentPosition(
        position => {
            const { latitude, longitude } = position.coords;
            console.log(latitude, longitude);
        },
        error => console.error("Location error:", error),
        { timeout: 5000 }
    );
}