API Geolocation
Geolocation asks the browser for a position after permission. Explain the purpose before requesting a sensitive capability.
Where to run this
Use a local browser project for this example. It needs browser permissions, additional files, a server endpoint, or a library as described below. The restricted playground cannot provide those dependencies.
A practical example
if ("geolocation" in navigator) {
navigator.geolocation.getCurrentPosition(
position => console.log(position.coords.accuracy),
error => console.log("Position unavailable:", error.code)
);
}
What happens
A permitted secure-origin test reports accuracy or a handled failure.