Was trying to test geolocation function in my React project on Firefox 74.0
on Linux Mint 19.3
.
When trying to execute the following code:
window.navigator.geolocation.getCurrentPosition(
position => console.log(position),
err => console.log(err)
);
The following error was shown in the Firefox browser console:
GeolocationPositionError { code: 2, message: "Unknown error acquiring position" }
About a year ago Google changed its policies, so now it requires a valid API key when accessing their geolocation service.
If you type in your Firefox address bar:
about:config
and search the following:
geo.provider.network.url
you will see that its value is set to
https://www.googleapis.com/geolocation/v1/geolocate?key=%GOOGLE_LOCATION_SERVICE_API_KEY%
This tells us that you need a valid API key in place of %GOOGLE_LOCATION_SERVICE_API_KEY%
Until Mozilla solves this issue internally, the best solution is to change that value to:
https://location.services.mozilla.com/v1/geolocate?key=test
Note: This solution is best for testing. For production you can try ajax calls using either fetch
or axios
in React.