【问题标题】:Callbacks for navigator.geolocation.getCurrentPosition are not firing in firefoxnavigator.geolocation.getCurrentPosition 的回调未在 Firefox 中触发
【发布时间】:2019-02-04 03:53:00
【问题描述】:

我在 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。火狐:61.0.1

我已经尝试过的事情:

  1. 通过 HTTPs 使用应用程序

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

  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-位置:假
    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


    【解决方案1】:

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

    中找到最合适的 here

    【讨论】:

    • 谢谢。我忘了提到我已经检查了 about:config。 geo.enabled 设置为 true。更新了我的问题。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2018-03-30
      • 2013-09-14
      • 1970-01-01
      • 2012-03-02
      • 2010-11-03
      • 2011-02-12
      • 2020-08-08
      • 1970-01-01
      相关资源
      最近更新 更多