Home > Mobile >  React Native - unable to get current location
React Native - unable to get current location

Time:12-31

I want to get current location when the user has the "get current location" checkbox checked.

I have a form for user inputs and one of them is a checkbox that gets the users current location when the checkbox is checked and the form is submitted.

My Code:

const getLocation = () => {
console.log("Testing getLocation");
(async () => {
  console.log("Ask Location Permission");
  let { status } = await Location.requestForegroundPermissionsAsync();
  console.log("Permission Asked");
  if (status !== "granted") {
    setErrorMsg("Location access denied!");
    console.log(errorMsg);
    return;
  }
  let currentLocation = await Location.getCurrentPositionAsync({});
  setLatitude(currentLocation.coords.latitude);
  setLongitude(currentLocation.coords.longitude);
  console.log("Start of Print Coords");
  console.log(currentLatitude);
  console.log(currentLongitude);
  console.log("End of Print Coords");
})();
};

const onSubmit = (data: { distance: number; coordinates: string }) => {
if (checkbox) {
  console.log("Checkbox Selected");
  getLocation();
  console.log("getLocation Tested");
  coords.push(currentLatitude);
  coords.push(currentLongitude);
  console.log(coords);
}

When i submit the form, I have multiple console logs to check where my issue is as it never asks to get location permissions. In the getLocation, it exits after printing "Ask Location Permission" and somehow comes back to print "Permission Asked" when the app does not ask for permission for location.console logs

I am using expo location to get current location and i dont use useEffect as i only need the current coordinates once and it does not need to be updated. https://docs.expo.dev/versions/latest/sdk/location/

CodePudding user response:

In your case, async funcation is merely declared in getLocation(). You need to call that function like below.

const getLocation = () => {
console.log("Testing getLocation");
async () => {
  console.log("Ask Location Permission");
  let { status } = await Location.requestForegroundPermissionsAsync();
  if (status !== "granted") {
    setErrorMsg("Location access denied!");
    console.log(errorMsg);
    return;
  }
  let currentLocation = await Location.getCurrentPositionAsync({});
  setLatitude(currentLocation.coords.latitude);
  setLongitude(currentLocation.coords.longitude);
  console.log(currentLatitude);
  console.log(currentLongitude);
}(); // here you need to call it using parenthesis
};

CodePudding user response:

There's a situation when previously the user choose not this app ask permission anymore. Next time the app asks this permission, silently device return status: "denied" without permission popup modal.

Always check whether the app eligible to ask that permission again.

  let { status,canAskAgain } = await Location.requestForegroundPermissionsAsync();
  console.log("Permission Asked");


if(canAskAgain === false){

// User has previously blocked app to ask this permission
// Force user to grant permission manually

 await Linking.openSettings();

}
  if (status !== "granted") {
    setErrorMsg("Location access denied!");
    console.log(errorMsg);
    return;
  }

  • Related