I just try to use expo-location and I found that error Location.requestForegroundPermissionsAsync is not a function
this is my code
import * as Location from 'expo-location';
const setCurrentLocation = async() => {
let { status } = await Location.requestForegroundPermissionsAsync();
if (status !== 'granted') {
setErrorMsg('Permission to access location was denied');
return;
}
let location = await Location.getCurrentPositionAsync({});
setLocation(location);
if (errorMsg) {
setCheckText(errorMsg)
} else if (location) {
setCheckText(JSON.stringify(location))
}
}
To display the coordinates -
state
like thisimport { useState } from "react";
.
.
.
const [location, setLocation] = useState(null);
setCurrentLocation
function as shownconst setCurrentLocation = async () => {
let { status } = await Location.requestForegroundPermissionsAsync();
if (status !== 'granted') {
setErrorMsg('Permission to access location was denied');
return;
}
let location = await Location.getCurrentPositionAsync({});
setLocation(location);
};
render
part will look something like thisreturn (
<View style={styles.container}>
{location ? <Text>latitude = {location.coords.latitude}</Text> : null}
{location ? <Text>longitude = {location.coords.longitude}</Text> : null}
</View>
);