Samuli Hakoniemi
Samuli Hakoniemi

Reputation: 19049

GPS stays on although app is moved to background on Android

I have a problem with GPS which stays on and keeps polling although my React Native app is moved to background. This is functioning properly on iOS, but on Android nothing happens.

The code looks like:

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() is executed when AppState equals background. This is proofed to work and it's actually called both on iOS and Android. But for some reason Android fails with it.

I couldn't find any similar question on SO and no open issues on RN.

Upvotes: 0

Views: 341

Answers (1)

Samuli Hakoniemi
Samuli Hakoniemi

Reputation: 19049

This was solved. The root cause is in react-native-mapbox-gl and 4 days ago there was a reported issue about it: https://github.com/mapbox/react-native-mapbox-gl/issues/310

Upvotes: 1

Related Questions