watchPosition is not working for me on Either Android and iOS
See original GitHub issueI 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?
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:8 (2 by maintainers)
Top Related StackOverflow Question
I have followed the exact example code, given all permissions on both Android and iOS but nothing is working
I’m using this one as well, but not able to get updated location