React Native watchPosition not updating even after moving about 370m (android)

I am currently trying to get an android app to detect its position and then load the latitude and longitude into firebase every 10 seconds. Loading seems fine at the moment, but navigator.geolocation.watchPosition doesn't seem to update even after moving 370m, even though it should update every 10m.

I've tried testing both in an emulator, changing latitude and longitude, and also in real life on a 370 meters bike.

Oh, and yes, I have included permissions for the FINE location in the android manifest. I also tried testing to see if geolocation works at all and it seems to update if I emulate myself in London and then Oxford. Therefore, there is no problem in detecting massive changes in location, but not if the difference in location is only 370m, say one end of the road to the other.

Any help would be really appreciated !. Thanks in advance!

 import React, {
  Component,
} from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  ScrollView
} from 'react-native';

let myDatas={
  timestamp:[],
  longitude:[],
  latitude:[],
};

export default class GeoLocation extends Component {
  constructor() {
    super();

    this.state = {
      latitude: null,
      longitude: null,
      timestamp: null,
      error: null,
    };
  }

  componentDidMount() {
var watchPositionID;


    watchPositionID = navigator.geolocation.watchPosition(
      (position) => {
        let now = new Date()
        myDatas.longitude.push(position.coords.longitude);
        myDatas.latitude.push(position.coords.latitude);
        myDatas.timestamp.push(now);
        this.setState({
          latitude: position.coords.latitude,
          longitude: position.coords.longitude,
          timestamp: now,
          error: null,
        });
      },
      (error) => this.setState({error:error.message}),
      { enableHighAccuracy: true, timeout: 20000, maximumAge: 0, distanceFilter: 10},
      );


this.uploadTimer=setInterval(
  ()=>{
    uploadData(myDatas);
    myDatas={
      timestamp:[],
      longitude:[],
      latitude:[],
    };
  },10000
  )

}

componentWillUnmount() {
  navigator.geolocation.clearWatch(watchPositionID);
  this.uploadTimer && clearInterval(this.uploadTimer);
  this.timerpos && clearInterval(this.timerpos);
}



render() {
  return (
    <View>
    <Text>Latitude: {this.state.latitude}</Text>
    <Text>Longitude: {this.state.longitude}</Text>
    </View>
    );
}
}

      

+3


source to share





All Articles