So I'm trying to get the users current co-oridnates from expo-location and testing it using the expo client on my android phone. The code below logs status as approved but latitude and longitude don't get updated and on iphone I get the following error:
[Unhandled promise rejection: Error: Request failed with status code 400] at node_modules/axios/lib/core/createError.js:15:17 in createError at node_modules/axios/lib/core/settle.js:16:9 in settle at node_modules/axios/lib/adapters/xhr.js:52:6 in handleLoad at node_modules/react-native/Libraries/Network/XMLHttpRequest.js:601:4 in setReadyState at node_modules/react-native/Libraries/Network/XMLHttpRequest.js:396:6 in __didCompleteResponse at node_modules/react-native/Libraries/vendor/emitter/_EventEmitter.js:135:10 in EventEmitter#emit
Code:
const [latitude, setLatitude] = useState();
const [longitude, setLongitude] = useState();
const getLocationAsync = async () => {
let { status } = await Location.requestForegroundPermissionsAsync();
if (status === 'granted') {
console.log('Approved!');
return Location.getCurrentPositionAsync({ enableHighAccuracy: true });
} else {
console.log('Rejected!');
throw new Error('Location permission not granted');
}
}
useEffect(() => {
const location = getLocationAsync();
setLatitude(location.latitude);
setLongitude(location.longitude);
}, []);
getLocationAsync is promise function.
you can change this code
useEffect(() => {
(async () => {
const { status } = await Location.requestForegroundPermissionsAsync();
if (status === 'granted') {
console.log('Approved!');
const location = await Location.getCurrentPositionAsync({ enableHighAccuracy: true });
setLatitude(location.latitude);
setLongitude(location.longitude);
} else {
console.log('Rejected!');
throw new Error('Location permission not granted');
}
})();
}, []);