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

问题描述 投票:0回答:2

我在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中没有任何navigator.geolocation.getCurrentPosition回调被触发。操作系统:MS Windows 10. Firefox:61.0.1

我已经尝试过做的事情:

  1. 使用HTTP上的应用程序
  2. PositionOptions对象的各种组合作为第三个参数传递给navigator.geolocation.getCurrentPosition。
  3. 将Firefox从61.0.1降级到53.0.3
  4. 摆弄about:config geo选项。例如,将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-location: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();
      }
javascript reactjs typescript firefox cross-browser
2个回答
0
投票

在浏览器中粘贴about:config,看看是否在那里启用了地理定位服务。最合适的你可以在Mozilla支持中找到here


0
投票

此问题是由同时使用react-geolocated lib和navigator.geolocation.getCurrentPosition()引起的。回调没有被解雇,因为firefox认为地理定位已经解决了。评论反应地理定位的lib使用有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.