如何强制手表位置获取位置

时间:2019-07-14 01:38:16

标签: react-native geolocation

我在第二次获取geoLocation时遇到问题。用户登录后,将在初始屏幕上使用以下代码。用户登录后,它将运行。但是,在用户注销然后再登录后,componentDidMount()函数将运行,但是地理位置不会调用成功函数或失败函数。这是因为我正在使用手表位置,因此没有新的坐标可用。如何强制watchPosition获取当前位置,以便可以运行来自Success的代码?我尝试修改甚至删除Maximumage,但是似乎没有任何作用。

  componentDidMount () {
     this.props.navigation.setParams({
        location: `Fetching Location`,
     });
     let geoOptions = {
        enableHighAccuracy: true,
        timeOut: 20000,
        maximumAge: 60 * 60 * 2
   } 

    navigator.geolocation.watchPosition(
      this.geoSuccess,
      this.geoFailure,
      geoOptions
    )
  }

  geoSuccess = position => {
     this.props.navigation.setParams({
            location: `${position.coords.latitude} ${position.coords.longitude}`,
        });
    this.setState({
      ready: true,
      where: { lat: position.coords.latitude, lng: position.coords.longitude }
    })

  }

  geoFailure = err => {
    this.setState({ error: err })
  }

我的注销代码是(如果应该在此处完成操作):

componentWillUnmount() {
    this.props.logout()
  }

  onLogoutAPIAsync = () => {
    this.props.navigation.navigate('Login')
    fetch(`http://localhost:3000/logout`, {...
}

0 个答案:

没有答案