ARTICLE AD BOX
I'm currently making an app for a school project and need to use location for navigating on the app. As stated in the title, it works just fine on iOS devices, and even the android emulator, but an actual android device seems to not grab location for some reason. When trying to obtain coordinates with something like currLocation.latitude and currLocation.longitude, it does not return on Android devices, but will return perfectly fine on iOS and emulator devices.
I can also clearly see my position on the Map page set up using Mapbox.
export function useLocationServices() { const [currLocation, setCurrLocation] = useState(null); const [locationError, setLocationError] = useState(null); // variable subscribing to locationServices const locationSubscription = useRef(null); useEffect(() => { async function foregroundTracking() { try { let { granted } = await Location.requestForegroundPermissionsAsync(); if (!granted == 'granted') { Alert.alert( "Navigation Error", [{ text: "OK" }] ); setLocationError("Location permission denied"); return; } // Start watching position in real time locationSubscription.current = await Location.watchPositionAsync( { accuracy: Location.Accuracy.Balanced, timeInterval: 500, distanceInterval: 1, }, (location) => { setCurrLocation(location.coords); setLocationError(null); } ); } catch (err) { console.error("Location tracking error:", err); setLocationError(err.message); Alert.alert( "Location Error", `Failed to access location services: ${err.message}`, [{ text: "OK" }] ); } } foregroundTracking(); // Cleanup subscription on unmount return () => { if (locationSubscription.current) { try { locationSubscription.current.remove(); } catch (e) { console.warn("Error removing location subscription:", e); } } }; }, []); return currLocation; }In another screen, I am using the same hook:
const currLocation = useLocationServices(); console.log(currLocation.latitude) console.log(currLocation.longitude)I'm really confused on why this is breaking on Android devices specifically.
My Expo and Expo-location version is:
