navigator.geolocation returns a Geolocation object — the Geolocation API entry point. Learn permissions, HTTPS rules, getCurrentPosition, watchPosition, five examples, and try-it labs.
01
Kind
Read-only property
02
Returns
Geolocation
03
Baseline
Widely available
04
Context
Secure (HTTPS)
05
One-shot
getCurrentPosition
06
Live
watchPosition
Fundamentals
Introduction
Maps, store finders, and weather widgets often need the user’s location. The Geolocation API lets your page request that location — but only after the browser asks the user for permission.
Your entry point is navigator.geolocation. It is read-only and returns a Geolocation object with methods such as getCurrentPosition() and watchPosition().
💡
Permission + HTTPS
MDN: location access notifies the user and requires permission. The API is available in secure contexts (HTTPS / localhost). Always handle “denied” and timeouts kindly.
Concept
Understanding the geolocation Property
Think of navigator.geolocation as a handle to the device’s location services for this page. You do not assign to it — you call methods on the object it returns.
getCurrentPosition(success, error?, options?) — one location reading.
watchPosition(success, error?, options?) — ongoing updates; returns a watch ID.
clearWatch(id) — stop a watch.
coords — on success, use position.coords.latitude / longitude (and more).
Foundation
📝 Syntax
General form of the property:
JavaScript
navigator.geolocation
Value
A Geolocation object that provides access to the device location.
Common patterns
JavaScript
if ("geolocation" in navigator) {
navigator.geolocation.getCurrentPosition(
(pos) => {
console.log(pos.coords.latitude, pos.coords.longitude);
},
(err) => {
console.log(err.code, err.message);
},
{ enableHighAccuracy: true, timeout: 10000, maximumAge: 0 }
);
} else {
// Geolocation not available
}
Cheat Sheet
⚡ Quick Reference
Goal
Code
Get Geolocation
navigator.geolocation
Detect API
"geolocation" in navigator
One reading
getCurrentPosition(success, error, options)
Watch updates
watchPosition(success, error, options)
Stop watching
clearWatch(id)
Secure page?
window.isSecureContext
Snapshot
🔍 At a Glance
Four facts to remember about navigator.geolocation.
Returns
Geolocation
API entry
Baseline
widely
Since ~2015
HTTPS
required
Secure context
Permission
user prompt
Must allow
Compare
📋 getCurrentPosition vs watchPosition
getCurrentPosition
watchPosition
Frequency
One-shot
Ongoing updates
Best for
“Find stores near me”
Live maps / tracking a walk
Cleanup
None
clearWatch(id)
Battery
Usually lighter
Can use more power
Permission
Both require user permission when needed
Hands-On
Examples Gallery
Examples follow MDN Geolocation / navigator.geolocation patterns. Prefer Try It Yourself over HTTPS and allow location when prompted. Use View Output for expected messaging.
navigator.geolocation is Baseline Widely available across modern browsers (MDN: since about July 2015). Always use a secure context and respect the permission prompt.
✓ Baseline · Widely available
Navigator.geolocation
Feature-detect, require HTTPS, call getCurrentPosition/watchPosition from a user gesture when possible, and handle denials.
UniversalWidely available
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
geolocationExcellent
Bottom line: Use navigator.geolocation for location features. Keep a non-location fallback when permission is denied.
Wrap Up
Conclusion
navigator.geolocation is the Geolocation API entry point. Feature-detect it, require HTTPS, request permission politely, use getCurrentPosition or watchPosition, and always handle errors.
Request location on every page load without context
Assume the user will always allow
Ignore timeout / unavailable errors
Track users continuously without a clear purpose
Forget battery cost of high-accuracy watches
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about geolocation
Location API entry — permission, HTTPS, get or watch.
5
Core concepts
📍01
Returns
Geolocation
API
🔍02
One-shot
getCurrentPosition
Read
🔄03
Live
watchPosition
Watch
🔒04
HTTPS
secure context
Security
👤05
Permission
user prompt
Consent
❓ Frequently Asked Questions
It is a read-only Navigator property that returns a Geolocation object — the entry point to the Geolocation API for reading the device’s location (with user permission).
No. MDN marks it Baseline Widely available (since about July 2015). It is not Deprecated, Experimental, or Non-standard. It does require a secure context (HTTPS / localhost).
Yes. When your page requests location, the browser asks for permission. Policies and UI differ by browser. Always handle denial gracefully.
Call navigator.geolocation.getCurrentPosition(success, error, options). The success callback receives a GeolocationPosition with coords.latitude and coords.longitude.
watchPosition(success, error, options) keeps updating as the device moves and returns a watch ID. Stop updates with clearWatch(id).
No. The property is read-only. You call methods on the returned Geolocation object.
Did you know?
MDN emphasizes that every browser has its own policies for asking location permission — so never hard-code assumptions about when the prompt appears or how denial is worded.