transistorsoft / react-native-background-geolocation

Sophisticated, battery-conscious background-geolocation with motion-detection
http://shop.transistorsoft.com/pages/react-native-background-geolocation
MIT License
2.66k stars 426 forks source link

Getting undefined for location coordinates. #2127

Closed fragilehm closed 2 months ago

fragilehm commented 2 months ago

Your Environment

/**
 * This should called initialized when the application starts,
 * the purpose of this hook is to get the background location services ready.
 */
const useBackgroundLocation = () => {
  const setReady = useBackgroundLocationStore(store => store.setReady);
  useEffect((): (() => void) | void => {
    const onLocation: Subscription = BackgroundGeolocation.onLocation(
      (location: Location): void => {
        if (!location.sample) {
          sendLocation({
            longitude: location.coords.longitude,
            latitude: location.coords.latitude,
            timestamp: location.timestamp,
            sample: location.sample,
          });
        }
      },
    );

    BackgroundGeolocation.ready({
      desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
      distanceFilter: 5,
      stopTimeout: 10,
      isMoving: true,
      logLevel: BackgroundGeolocation.LOG_LEVEL_VERBOSE,
      disableLocationAuthorizationAlert: true,
    }).then((_state: State): void => {
      setReady(true);
    });

    return (): void => {
      // Remove BackgroundGeolocation event-subscribers when the View is removed or refreshed
      // during development live-reload.  Without this, event-listeners will accumulate with
      // each refresh during live-reload.
      onLocation.remove();
    };
  }, [setReady]);
};

export default useBackgroundLocation;

Expected Behavior

When receiving a location update, the coordinates should be defined.

Actual Behavior

When receiving a location update, the location coordinates are undefined.

image

Steps to Reproduce

  1. Start the app,
  2. Allow background location tracking
  3. Allow fitness tracking
  4. Move to trigger location update.
christocracy commented 2 months ago

The .onLocation event takes a 2nd callback failure callback. If you don't provide the 2nd failure callback, then LocationError is sent to the 1st callback.

You can easily see this by console.log(location) to see that you're receiving a LocationError.

See the API docs for .onLocation

Provide a 2nd arg failure callback to your .onLocation(success, failure).

christocracy commented 2 months ago

You would have been seeing a ⚠️ in the logs about that too.