react-native-geolocation icon indicating copy to clipboard operation
react-native-geolocation copied to clipboard

watchPosition is not working for me on Either Android and iOS

Open muzammilideofuzion opened this issue 2 years ago • 10 comments

I want to get user's location after every second in app so that I can use latest coordinates to change my Map marker and somethings more, but this is not working for me, I have done everything as doc said and also have all the permission given on my mobile app but nothing is working for me. I have made a separate hook for watching position here is the code: `const useWatchLocation = () => { const dispatch = useDispatch(); const [subscriptionId, setSubscriptionId] = useState<number | null>(null);

const watchPosition = () => {
    try {
        const watchID = Geolocation.watchPosition(
            (position) => {
                let location: CURRENT_LOCATION = formatLocation(position.coords.latitude, position.coords.longitude);
                console.log('location', location);
                dispatch(setUserLocation(location));
            },
            (error) => { _showConsoleError(`Error while watching location: ${error}`) },
            {
                enableHighAccuracy: true,
                fastestInterval: 1000,
            }
        );
        setSubscriptionId(watchID);
    } catch (error) {
        _showConsoleError(`Error while watching location: ${error}`);
    }
};

const clearWatch = () => {
    subscriptionId !== null && Geolocation.clearWatch(subscriptionId);
    setSubscriptionId(null);
};

useEffect(() => {
    return () => {
        clearWatch();
    };
}, []);

return {
    watchPosition,
    clearWatch,
}

}

export default useWatchLocation`

You can see that I'm updating redux state upon on new location. I have used this location state as dependency in another screen, here is the code:

useEffect(() => { if (location && mapRef && mapRef.current) { let camera: Camera = { center: { latitude: location.latitude, longitude: location.longitude }, zoom: 18, altitude: undefined, heading: 0, pitch: 0, }; mapRef.current.animateCamera(camera); } }, [location])

I'm also showing coordinates using Text so that I can see that on moving location is also updating in mobile application but nothing is working, can anyone please help me out in this?

muzammilideofuzion avatar Nov 01 '22 13:11 muzammilideofuzion

Can you provide me with repro example?

michalchudziak avatar Nov 04 '22 15:11 michalchudziak

Watch Position is not working for me as well

AhsanSheikh02 avatar Nov 07 '22 10:11 AhsanSheikh02

Geolocation.watchPosition( position => { console.log('watchPosition', position); dispatch(UserLocation(position)) }, error => { console.log(error.message); }, { enableHighAccuracy: false, timeout: 10000, distanceFilter: 1, maximumAge: 1000, }, );

AhsanSheikh02 avatar Nov 07 '22 10:11 AhsanSheikh02

Could you give me a bit more context on how you use this code? Maybe a small repro project?

michalchudziak avatar Nov 07 '22 18:11 michalchudziak

I'm also facing this issue

Haseeba393 avatar Nov 21 '22 14:11 Haseeba393

You can use "react-native-geolocation-service"

AhsanSheikh02 avatar Nov 21 '22 14:11 AhsanSheikh02

I'm using this one as well, but not able to get updated location

Haseeba393 avatar Nov 21 '22 14:11 Haseeba393

I have followed the exact example code, given all permissions on both Android and iOS but nothing is working

Haseeba393 avatar Nov 21 '22 14:11 Haseeba393

@michalchudziak Can you please update the example with the latest RN version? Example's patch is not working on the 0.71.2 version. And pod install was failed on the current example app.

I tried to use the example app, as watchPosition didn't work on my app. It shows following error.

Sending geolocationDidChange with no listeners registered.

hristo0624 avatar Feb 13 '23 15:02 hristo0624

@hristo0624 I recommend you to install expo-location-package using expo-modules-package in bare project. Its really good package handles everything very nicely.

Haseeba393 avatar Feb 14 '23 00:02 Haseeba393