Careeroza
Geolocation
The Geolocation API
navigator.geolocation asks the user for their location. The browser always shows a permission prompt, and the API only works on HTTPS pages.
navigator.geolocation.getCurrentPosition(
(pos) => {
console.log('Latitude:', pos.coords.latitude);
console.log('Longitude:', pos.coords.longitude);
console.log('Accuracy (m):', pos.coords.accuracy);
},
(err) => console.log('Could not get location:', err.message),
{ enableHighAccuracy: true, timeout: 10000 },
);Tip: Ask for location only when the user does something that needs it (e.g. clicks "Find stores near me"), not on page load.