react-native-geolocation
react-native-geolocation copied to clipboard
watchPosition is not working for me on Either Android and iOS
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?
Can you provide me with repro example?
Watch Position is not working for me as well
Geolocation.watchPosition( position => { console.log('watchPosition', position); dispatch(UserLocation(position)) }, error => { console.log(error.message); }, { enableHighAccuracy: false, timeout: 10000, distanceFilter: 1, maximumAge: 1000, }, );
Could you give me a bit more context on how you use this code? Maybe a small repro project?
I'm also facing this issue
You can use "react-native-geolocation-service"
I'm using this one as well, but not able to get updated location
I have followed the exact example code, given all permissions on both Android and iOS but nothing is working
@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 I recommend you to install expo-location-package using expo-modules-package in bare project. Its really good package handles everything very nicely.