What is the Geolocation API used for?
The Geolocation API lets a web app ask "where is this device right now?" and get back coordinates — latitude and longitude — straight from the browser.
Plain JavaScript has no way to find out where you physically are; the page only knows what you typed. The Geolocation API bridges that gap by letting the browser tap into whatever positioning hardware the device has — GPS on a phone, or Wi-Fi and IP-based estimates on a laptop. That single capability — "the device knows it's at 47.05° N, 8.31° E" — is what powers a huge range of features:
- Route planning and navigation — a maps app needs your start point to draw a route.
- Booking services (Uber, AirBnB) — "find a ride/room near me" only works if "me" has a location.
- Weather forecasts — show the forecast for where you are without making you type a city.
- Sports tracking — record the path of a run by sampling position over time.
- Local search — "restaurants nearby", "closest ATM".
A key thing to understand is the round trip: the browser only gives you raw coordinates. To turn 47.05, 8.31 into something useful ("the three closest pizzerias"), your code typically sends those coordinates to a server which does the lookup and sends results back. The browser locates; the server interprets.
Because location is sensitive personal data, the browser always asks the user for permission first with a prompt, and the user can deny it. Your code must handle that "no" gracefully rather than assuming a position will always arrive. (This is also why geolocation only works over HTTPS — covered in its own card.)
Memory tip: the API answers where, the server answers so what.
Go deeper:
MDN — Geolocation API — the canonical overview: methods, the permission model, and the secure-context requirement.