Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

watchposition() issues #340

Open
SKHRAPP opened this issue Oct 24, 2024 · 1 comment
Open

watchposition() issues #340

SKHRAPP opened this issue Oct 24, 2024 · 1 comment
Labels
question Further information is requested

Comments

@SKHRAPP
Copy link

SKHRAPP commented Oct 24, 2024

timeout is not workling properly why ?

code

 let watchId;
 watchId = Geolocation.watchPosition(handleSuccess, handleError, {
        timeout: 1000,
        enableHighAccuracy: true,
        interval: 15 * 60 * 1000,
        maximumAge: 3000,
      });


  const handleError = error => {
        console.log(error, 'error dataaa');
        Geolocation.clearWatch(watchId);
      };

  const handleSuccess = async position => {
        let data = {
          latitude: parseFloat(position.coords.latitude).toFixed(6),
          longitude: parseFloat(position.coords.longitude).toFixed(6),
        };
        console.log(data, 'dataaaa');
     Geolocation.clearWatch(watchId);
      };
@SKHRAPP SKHRAPP added the question Further information is requested label Oct 24, 2024
@noobix
Copy link

noobix commented Oct 27, 2024

I also have watch position issues that I have been trying to fix for a month now. Arguably some people have issues of getting all of geolocation responses in particular heading, speed and timestamp. I have a post on stackoverflow
Please I need some help with this as soon as possible.
This is my code

React.useEffect(() => {
    Geolocation.requestAuthorization(
      (_success?: any) => {
        console.log("Location permission requested successfully");
      },
      (error?: GeolocationError) => {
        console.log("Error requesting location permission:", error);
      }
    );
    if (!origin && !destination && !recordingMode) {
      if (watchId !== null) {
        Geolocation.clearWatch(watchId);
        setWatchId(null);
      }
      const noSaveWatchId = Geolocation.watchPosition(
        (position: GeolocationResponse) => {
          const {
            latitude,
            longitude,
            altitude,
            accuracy,
            speed,
            heading: geoHeading,
            altitudeAccuracy,
          } = position.coords;
          const { timestamp } = position;

          setCurrentPosition({
            coordinate: { latitude, longitude },
            altitude: altitude as number,
            timestamp: position.timestamp,
            accuracy: accuracy,
            speed: speed as number,
            heading: heading || (geoHeading as number),
            altitudeAccuracy: altitudeAccuracy as number,
            isFromMockProvider: (position as any).mocked,
            timeStamp: timestamp,
          });
        },
        (error: GeolocationError) => console.log(error),
        {
          enableHighAccuracy: true,
          distanceFilter: 0,
          interval: 5000,
          fastestInterval: 2000,
        }
      );
      setWatchId(noSaveWatchId);

      return () => {
        if (noSaveWatchId !== null) {
          Geolocation.clearWatch(noSaveWatchId);
        }
      };
    } else if (origin && destination && recordingMode) {
      if (watchId !== null) {
        Geolocation.clearWatch(watchId);
        setWatchId(null);
      }
      const saveWatchId = Geolocation.watchPosition(
        (position: GeolocationResponse) => {
          const {
            latitude,
            longitude,
            altitude,
            accuracy,
            speed,
            heading: geoHeading,
            altitudeAccuracy,
          } = position.coords;
          const { timestamp } = position;

          setCurrentPosition({
            coordinate: { latitude, longitude },
            altitude: altitude as number,
            timestamp: position.timestamp,
            accuracy: accuracy,
            speed: speed as number,
            heading: heading || (geoHeading as number),
            altitudeAccuracy: altitudeAccuracy as number,
            isFromMockProvider: (position as any).mocked,
            timeStamp: timestamp,
          });

          if (
            state &&
            state.location.lat !== latitude &&
            state.location.lng !== longitude
            // state.location.heading !== heading
          ) {
            dispatch({
              type: Types.SetLocation,
              payload: {
                lat: latitude ?? 0,
                lng: longitude ?? 0,
                heading: heading ?? 0,
              },
            });
          } else return;
          makeLogEntry({
            latitude,
            longitude,
            heading: heading || undefined,
          })
            .then(() => console.log("Log made successfully"))
            .catch((error) => console.log("Error making log entry:", error));
        },
        (error: GeolocationError) => console.log(error),
        {
          enableHighAccuracy: true,
          distanceFilter: 0,
          interval: 5000,
          fastestInterval: 2000,
        }
      );
      setWatchId(saveWatchId);

      return () => {
        if (saveWatchId !== null) {
          Geolocation.clearWatch(saveWatchId);
        }
      };
    }
  }, [origin, destination, recordingMode]);

I am able to receive lng and lat accurately but heading is always '0'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants