navigator.geolocation.getCurrentPosition 的回调未在 firefox 中触发

Callbacks for navigator.geolocation.getCurrentPosition are not firing in firefox

我在 React Component 中有一个函数,它负责获取用户的位置。

    export default class SomeComponent extends React.Component<SomeProps, SomeState> {
    constructor(props: any) {
    super(props)
    //initializing state here...
    }
      componentDidMount() {
        this.getCurrentPosition()
      }

    getPosition = () => {
    console.log('get current position') //it's firing
      navigator.geolocation.getCurrentPosition((position: Position) => {
        console.log('pos', position) // not firing in any case
        //do stuff here..
      },
        (error: any) => {
          console.log(error) //not firing in any case
            //do stuff here..
        })
    }
}

我的问题是这种方法在 Chrome/Edge 中工作得很好,但在 Firefox 中 none 的 navigator.geolocation.getCurrentPosition 回调正在触发。 OS: MS Windows 10. Firefox: 61.0.1

我已经尝试过的事情:

  1. 通过 HTTPs 使用应用程序

  2. 作为第三个参数传递给 navigator.geolocation.getCurrentPosition 的 PositionOptions 对象的各种组合。

  3. 正在将 firefox 从 61.0.1 降级到 53.0.3

  4. 摆弄 about:config 地理选项。例如,改变 geo.wifi.uri 来自 https://www.googleapis.com/geolocation/v1/geolocate?key=%GOOGLE_API_KEY%https://location.services.mozilla.com/v1/geolocate?key=%MOZILLA_API_KEY%

    当前地理位置选项:
    geo.enabled: 真
    geo.provider.ms-windows-位置:false
    geo.wifi.uri: https://www.googleapis.com/geolocation/v1/geolocate?key=%GOOGLE_API_KEY%
    geo.wifi.xhr.timeout: 60000

5 以普通 javascript 方式重新组装 getPosition 并从 tsx 组件调用它。

 // navigator.js

    function succ(pos){
       console.log('pos', pos)
    }
    function err(err){
       console.log('err', err)
    }
    export default function Nav(){
       console.log('nav')
       navigator.geolocation.getCurrentPosition(succ, err);
    }

    //SomeComponent.tsx
    import Nav from '../containers/navigator.js'
      componentDidMount() {
        Nav();
      }

about:config 粘贴到您的浏览器中,查看是否启用了地理定位服务。您可以在 Mozilla 支持

中找到最合适的 here

此问题是由同时使用 react-geolocated 库和 navigator.geolocation.getCurrentPosition() 引起的。回调没有触发可能是因为 firefox 认为地理定位已经解决。 注释掉 react-geolocated lib 用法有帮助。