---
id: html-geolocation
title: "HTML Geolocation"
category: "Frontend"
status: "draft"
verification_status: "conceptual"
canonical_id: ""
aliases: ["Geolocation API", "navigator.geolocation", "getCurrentPosition", "watchPosition", "HTML location API"]
duplicate_of: ""
source_trust_level: "B"
confidence_score: 0.89
created_at: 2026-06-23
updated_at: 2026-06-23
review_reason: ""
merge_history: []
tags: ["html", "web", "frontend", "w3schools", "geolocation", "api", "html5"]
raw_sources: ["https://www.w3schools.com/html/html5_geolocation.asp"]
applied_in: []
github_commit: ""
---
# [[HTML Geolocation]]
## π― ν μ€ ν΅μ°° (One-line insight)
The HTML Geolocation API locates a user's position (latitude and longitude) via `navigator.geolocation.getCurrentPosition()`, but only with the user's explicit permission and most accurately on GPS-equipped devices. [S1]
## π§ ν΅μ¬ κ°λ
(Core concepts)
- **`navigator.geolocation`** β the entry point to the Geolocation API; check for its existence to detect support. [S1]
- **User permission required** β the API will not return a location until the user grants permission. [S1]
- **`getCurrentPosition(success, error)`** β requests the current position once, calling a success or error callback. [S1]
- **`watchPosition()`** β continuously tracks the position as the user moves. [S1]
- **Accuracy depends on hardware** β works most accurately on devices with GPS (e.g. smartphones, smartwatches). [S1]
## π§© μΆμΆλ ν¨ν΄ (Extracted patterns)
- **Feature-detect first** β `if (navigator.geolocation) { ... } else { /* not supported */ }`. [S1]
- **Two-callback pattern** β pass a success handler that reads `position.coords.*` and an error handler that inspects `error.code`. [S1]
- **Error switch** β branch on `error.code` against `PERMISSION_DENIED`, `POSITION_UNAVAILABLE`, `TIMEOUT`, `UNKNOWN_ERROR`. [S1]
## π μΈλΆ λ΄μ© (Details)
**Locating the user's position**
The Geolocation API is used to get the geographical position of a user. Because this can compromise privacy, the position is not available unless the user approves it. It is most accurate for devices with GPS, like smartphones. Use the `getCurrentPosition()` method to get the user's position. [S1]
**Basic example**
```javascript
```
The example checks if geolocation is supported, runs `getCurrentPosition()` if so, and on success outputs the latitude and longitude. [S1]
**Handling errors**
The second parameter of `getCurrentPosition()` is used to handle errors. It specifies a function to run if it fails to get the user's location. The error callback can branch on `error.code`: [S1]
```javascript
function error(error) {
switch(error.code) {
case error.PERMISSION_DENIED:
x.innerHTML = "User denied the request for Geolocation."
break;
case error.POSITION_UNAVAILABLE:
x.innerHTML = "Location information is unavailable."
break;
case error.TIMEOUT:
x.innerHTML = "The request to get user location timed out."
break;
case error.UNKNOWN_ERROR:
x.innerHTML = "An unknown error occurred."
break;
}
}
```
**`getCurrentPosition()` return data**
The `getCurrentPosition()` method returns an object on success. The latitude, longitude, and accuracy properties are always returned; the other properties are returned if available. [S1]
| Property | Returns |
|---|---|
| `coords.latitude` | The latitude as a decimal number (always returned) |
| `coords.longitude` | The longitude as a decimal number (always returned) |
| `coords.accuracy` | The accuracy of position (always returned) |
| `coords.altitude` | The altitude in meters above the mean sea level (returned if available) |
| `coords.altitudeAccuracy` | The altitude accuracy of position (returned if available) |
| `coords.heading` | The heading as degrees clockwise from North (returned if available) |
| `coords.speed` | The speed in meters per second (returned if available) |
| `timestamp` | The date/time of the response (returned if available) |
**`watchPosition()`**
The `watchPosition()` method returns the current position of the user and continues to return updated position as the user moves (like the GPS in a car). The matching `clearWatch()` method stops the `watchPosition()` method. [S1]
```javascript
```
**Secure context note**
The Geolocation API requires a secure context such as HTTPS. [S1]
## π οΈ μ μ© μ¬λ‘ (Applied in summary)
The success/error and watch examples above are the canonical applied cases: a one-shot location lookup and continuous tracking, each with full error handling. A dedicated map-display example and a separate location-specific information sample are described conceptually but not provided as code in the source. No external project/commit applications found in the source.
## π» μ½λ ν¨ν΄ (Code patterns)
Feature detection + request (JavaScript):
```javascript
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(success, error);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
```
Reading coordinates in the success callback (JavaScript):
```javascript
function success(position) {
x.innerHTML = "Latitude: " + position.coords.latitude +
"
Longitude: " + position.coords.longitude;
}
```
## βοΈ λͺ¨μ λ° μ
λ°μ΄νΈ (Contradictions & updates)
No contradictions found in the source. Privacy and security constraints shape usage: a location is never returned without user approval, and the API requires a secure context (HTTPS). [S1]
## β
κ²μ¦ μν λ° μ λ’°λ
- **μν:** draft
- **κ²μ¦ λ¨κ³:** conceptual (μ€μ μ μ© μ¬λ‘ λ°κ²¬ μ applied/validatedλ‘ μΉκ²© κ°λ₯)
- **μΆμ² μ λ’°λ:** B (W3Schools β widely used educational reference, not a primary standards body)
- **μ λ’° μ μ:** 0.89
- **μ€λ³΅ κ²μ¬ κ²°κ³Ό:** μ κ· μμ± (New discovery)
## π μ§μ κ·Έλν (Knowledge Graph)
- **μμ/루νΈ:** [[HTML Tutorial]]
- **κ΄λ ¨ κ°λ
:** [[HTML Web APIs]], [[HTML Web Storage]], [[HTML Web Workers]], [[HTML Drag and Drop]]
- **μ°Έμ‘° λ§₯λ½:** Referenced whenever an app needs the user's physical location (maps, local search, geofencing).
## π μΆμ² (Sources)
- [S1] W3Schools β HTML Geolocation β https://www.w3schools.com/html/html5_geolocation.asp
## π λ³κ²½ μ΄λ ₯ (Change history)
- 2026-06-23: Initial draft synthesized from the W3Schools "HTML Geolocation" page (Astra wiki-curation, P-Reinforce v3.1 format).