expo: Location services in Android not working
I am using Location services in my react native Expo app. This is my code snippet.
`
let { status } = await Permissions.askAsync(Permissions.LOCATION);
if (status !== 'granted') {
this.setState({
errorMessage: 'Permission to access location was denied',
});
} else{
console.log( "Permission to access location was granted");
}
let that = this;
let {location} = await Location.watchPositionAsync({timeInterval:1000}, function(location){
console.log( "Location = " + JSON.stringify( location ) );
that.setState({ location:location });
});
` I would expect the code to get the location of the user every 1000ms ( 1 second ). However, 2 things happens -
- In the iOS simulator, it gets the real location the first time but doesnt get the location every 1 second.
- In the Android simulator ( Genymotion, any version of Android ), the await function never returns and the location is never obtained.
Point to note : In the Android emulator I keep getting the error
Android Geocoder not present. Please check if Geocoder.isPresent() before invoking the search
Could this have something to do with the location services not getting triggered?
This is a major impediment with testing any location based service using Expo. Can someone point out if I am using the API incorrectly?
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 17 (4 by maintainers)
Commits related to this issue
- [docs] Set canonical URLs in attempt to fix Google indexing (#262) (#436) — committed to expo/expo by abi 7 years ago
This issue is with android when location service is disabled and permission is granted… So to solve this … first use Expo.Location.getProviderStatusAsync()… it return an object… inside the object there are 4 fields… for this task we need only two: locationServicesEnabled (boolean), gpsAvailable (boolean) …check their boolean value… if any of them is false…Expo.LocationgetCurrentPositionAsync({}) … will give promise rejection error… so to not to get this error… we have to enable ’ locationServicesEnabled (boolean), gpsAvailable (boolean) ’ …To enable these… we have to use Expo.IntentLauncherAndroid. to open the location setting and enable it… to Open location setting… required code is:
import { IntentLauncherAndroid } from ‘expo’; // Open location settings IntentLauncherAndroid.startActivityAsync( IntentLauncherAndroid.ACTION_LOCATION_SOURCE_SETTINGS );
Then call Expo.getCurrentPositionAsync() … now you will get your location…
In my case, doing
Location.getCurrentPositionAsync({ enableHighAccuracy: true })does the trick (I’m on Genymotion)hi, this is spaghetti code because I, not enough time.but it will solve your problems. I tested it . and worked correctly.
Can confirm.
Android 6, physical device:
console output:
Location is not returned, no error.
Is it working when you test on a Physical Device ? Location services on Emulators are buggy.
This was resolved after I set my Google Geocoding API key… https://docs.expo.io/versions/latest/sdk/location.html#expolocationsetapikeyapikey
Get your key here: https://developers.google.com/maps/documentation/geocoding/start