Geolocation API
Explain how the interface should behave if the visitor denies permission.
Understand the topic
Geolocation requests the device location with user permission in an allowed secure context. Ask only when the visitor performs an action that needs location and explain the purpose. getCurrentPosition supplies coordinates through a success callback and reports failure through an error callback. Accuracy varies; a coordinate estimate is not proof of an exact address.
Worked example
if ("geolocation" in navigator) {
navigator.geolocation.getCurrentPosition(
({ coords }) => console.log(coords.latitude, coords.longitude, coords.accuracy),
(error) => console.log("Location unavailable:", error.message),
{ timeout: 10000, maximumAge: 60000 }
);
}
Run this from a deliberate user action on your own HTTPS page or permitted local development context. The course preview sandbox intentionally does not request location permission.
Practice
Explain how the interface should behave if the visitor denies permission.
Show a suggested answer
Show a clear message and allow manual location entry. Do not repeatedly prompt or prevent unrelated tasks.Common mistake
Do not silently collect location on page load or assume sandboxed iframes have permission to use the API.