Location with Expo


#1

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.


#2

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.


#3

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


#4

Not sure the URIs are consistent enough for that to work: https://stackoverflow.com/questions/38893684/linking-with-android-os-settings-in-react-native/39229693#39229693
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 https://expo.canny.io/feature-requests


#5

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


#6

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

RN 0.54
expo sdk 25


#7

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


#8

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


#9

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.


#10

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


#11

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.


#12

Problem exists on standalone app …