Location with Expo

I am using the code from the documentation provided here: https://docs.expo.io/versions/latest/sdk/location.html

I used the code sample in the documentation and I am supposed to get location permission access prompt and when granted the location services should be automatically enabled on the device to get the current location.

On my Android S6 edge test device I only get the prompt and when I press allow I get a warning that location services are not enabled.

So is there a way to programatically enable location services ?

Here is my code:

constructor(props) {
super(props);
this.state = {
location: false,
};
}

componentDidMount() {
this.askForUserPermissionToGetCurrentLocation();
}

async askForUserPermissionToGetCurrentLocation() {
// Ask for user’s permission to access location settings
let { status } = await
Expo.Permissions.askAsync(Expo.Permissions.LOCATION);
console.log(status);
// User denied access
if (status !== “granted”) {
console.log(“denied”);

// Otherwise, access granted
} else {
console.log(“permission granted”);
// Get the location
let location = await Expo.Location.getCurrentPositionAsync({});
console.log(location);
this.setState({ location: location });
}
}

Render method is just a text to display the longitude and latitude of the returned location.

Ah right now we have no API for enabling location services programmatically… You could open the settings page URI with the Linking API to have the user enable it from settings.

Can you direct me on how to get to do that ?

Not sure the URIs are consistent enough for that to work: Linking with android os settings in react native - Stack Overflow
You can just ask the user to enable location or detach if taking them directly to the settings is mission critical

Providing access to that seems like it would be useful to the community at large, mind creating a request on Feature Requests | Expo

Detaching is not an option unfortunately. Happy to make the request though … Thanks for your help.

i am not even getting the prompt…
even with the same code

RN 0.54
expo sdk 25

The code in the expo documentation is working fine, you just have to toggle the location ‘on’ from the drop down bar or from settings, keep in mind that you also have to change method for getting location (“Mode” option in android>8) and change it to “battery saving” or “high accuracy” (By default it is set to “device only”). The default setting will not fetch any location from device. The reason is yet unknown to me but changing it worked in my case. peace

1 Like

and how would I make sure that the user has the location mode set to battery saving or high accuracy ?

you can do this by following this path (may vary in different android/mobile versions)
settings > security&location > location > mode
you will get three options, choose any except device only.

But this I don’t have control of … I cannot just tell my users to do so … there has to be a way to make them do this programmatically

I think this is only just an issue with expo because our app is in development state. it will be fine once you package the app, though i still have not tested it myself but you can try.

p.s: android will still ask the user permission to allow your app to access location services of the device. i dont think/or know that you can bypass it.

Problem exists on standalone app …