--- 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).