我在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
我已经尝试过做的事情:
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 lib和navigator.geolocation.getCurrentPosition()引起的。回调没有被解雇,因为firefox认为地理定位已经解决了。评论反应地理定位的lib使用有帮助。