Agontuk / react-native-geolocation-service

React native geolocation service for iOS and android
https://www.npmjs.com/package/react-native-geolocation-service
MIT License
1.61k stars 291 forks source link

IOS watchPosition triggered only once. [Help Needed] #309

Open kamalpandey opened 3 years ago

kamalpandey commented 3 years ago

@Agontuk I am using the example code. In Android when I press Start Observing button it is working fine but in IOS it is triggered only once.

Am I missing some configuration or this is valid?

Screen Shot 2021-09-04 at 17 52 57
gitmazzio commented 3 years ago

follow

yaroslav-vasilyev commented 7 months ago

Hey, buddy. Found how you can adapt to this if you have the location only come once when watchPosition. Use setTimeout in parallel with this method, which will get the location from getCurrentPosition and everything will work as it should.

`const getLocationUpdates = async () => { const hasPermission = await hasLocationPermission();

if (!hasPermission) {
  return;
}

if (Platform.OS === 'android') {
  await startForegroundService();
}

if (Platform.OS === 'ios') {
  intervalId.current = setInterval(() => {
    getLocation();
  }, 5000);
}

watchId.current = Geolocation.watchPosition(
  position => {
    setLocation(position);
    console.log(position);
  },
  error => {
    setLocation(null);
    console.log(error);
  },
  {
    accuracy: {
      android: 'high',
      ios: 'best',
    },
    enableHighAccuracy: true,
    distanceFilter: 0,
    interval: 5000,
    fastestInterval: 2000,
    forceRequestLocation: true,
    forceLocationManager: true,
    showLocationDialog: true,
    useSignificantChanges: true,
  },
);

};`

duyrk commented 2 months ago

Hey, buddy. Found how you can adapt to this if you have the location only come once when watchPosition. Use setTimeout in parallel with this method, which will get the location from getCurrentPosition and everything will work as it should.

`const getLocationUpdates = async () => { const hasPermission = await hasLocationPermission();

if (!hasPermission) {
  return;
}

if (Platform.OS === 'android') {
  await startForegroundService();
}

if (Platform.OS === 'ios') {
  intervalId.current = setInterval(() => {
    getLocation();
  }, 5000);
}

watchId.current = Geolocation.watchPosition(
  position => {
    setLocation(position);
    console.log(position);
  },
  error => {
    setLocation(null);
    console.log(error);
  },
  {
    accuracy: {
      android: 'high',
      ios: 'best',
    },
    enableHighAccuracy: true,
    distanceFilter: 0,
    interval: 5000,
    fastestInterval: 2000,
    forceRequestLocation: true,
    forceLocationManager: true,
    showLocationDialog: true,
    useSignificantChanges: true,
  },
);

};`

What does getLocation() function actually do? Is it fetching current coordinate? Pls help, I faced the same issue above.