虽然应用已在Android

时间:2016-03-21 08:37:02

标签: android gps geolocation react-native

虽然我的React Native应用程序已移至后台,但我仍然遇到GPS问题并保持轮询。这在iOS上运行正常,但在Android上没有任何反应。

代码如下:

componentDidMount() {
  const { dispatch } = this.props

  navigator.geolocation.getCurrentPosition((initialPosition) => {
    dispatch(setUserLocation(initialPosition.coords))

    this.setState({
      locationError: false,
      position: initialPosition
    })
  }, (error) => {
    this.setState({
      locationError: true
    })
  }, {
    enableHighAccuracy: true,
    timeout: 20000,
    maximumAge: 1000
  })

  this.startWatchingPosition()
}

startWatchingPosition() {
  this.watchID = navigator.geolocation.watchPosition((lastPosition) => {
    if (this.state.position) {
      let distance = geolib.getDistance(this.state.position.coords, lastPosition.coords)
      if (distance > 250) {
        this.setState({
          locationError: false,
          position: lastPosition
        })
        dispatch(setUserLocation(lastPosition.coords))
      }
    }
    else {
      this.setState({
        locationError: false,
        position: lastPosition
      })
    }
  })
}

stopWatchingPosition() {
  navigator.geolocation.clearWatch(this.watchID)
  navigator.geolocation.stopObserving()
}
stopWatchingPosition()等于背景时,会执行

AppState。这可以证明是有效的,并且它实际上在iOS和Android上都被称为。但由于某种原因,Android失败了。

我无法在SO上找到任何类似的问题,也没有关于RN的公开问题。

1 个答案:

答案 0 :(得分:1)

这已经解决了。根本原因在private String getPlaceDetailsUrl(String ref){ // reference of place String reference = "reference="+ref; // Sensor enabled String sensor = "sensor=false"; // Building the parameters to the web service String parameters = reference+"&"+sensor+"&"+mKey; // Output format String output = "json"; // Building the url to the web service String url = "https://maps.googleapis.com/maps/api/place/details/"+output+"?"+parameters; return url; } ,4天前有报道的问题:https://github.com/mapbox/react-native-mapbox-gl/issues/310