未知错误获取位置 GeolocationPositionError 代码 2 - Firefox Linux Mint

Unknown error acquiring position GeolocationPositionError code 2 - Firefox Linux Mint

试图在 Linux Mint 19.3Firefox 74.0 上测试我的 React 项目中的地理定位功能。

尝试执行以下代码时:

 window.navigator.geolocation.getCurrentPosition(
        position => console.log(position),
        err => console.log(err)
    );

Firefox 浏览器控制台中显示以下错误:

GeolocationPositionError { code: 2, message: "Unknown error acquiring position" }

大约一年前 Google 更改了政策,因此现在在访问其地理定位服务时需要有效的 API 密钥。

如果您在 Firefox 地址栏中输入:

about:config

并搜索以下内容:

geo.provider.network.url

你会看到它的值设置为

https://www.googleapis.com/geolocation/v1/geolocate?key=%GOOGLE_LOCATION_SERVICE_API_KEY%

这告诉我们您需要一个有效的 API 密钥来代替 %GOOGLE_LOCATION_SERVICE_API_KEY%

在 Mozilla 内部解决此问题之前,最好的解决方案是将该值更改为:

https://location.services.mozilla.com/v1/geolocate?key=test



注意:此解决方案最适合测试。对于生产,您可以在 React 中使用 fetchaxios 尝试 ajax 调用。