I’m making an app with the react-native expo, and I have a MapView that is supposed to center on current location initially. On ios, the centering is done automatically (center called in component did mount), but on android, centering only occurs when I call the centerLocation method via a button after the component has been mounted. I’ve been able to narrow down the issue to the fact that getting the current location initially never resolves the first call, but I’m unsure why this happens, and why it works fine the second time I call the method. Permissions are all granted and check out fine. Any help would be appreciated
My get current location method call: (either way, through Location or through the navigator, doesn’t resolve the first call. It’s worth noting that this works completely fine on iOS)
const getCurrentLocation = async () => {
const position = Location.getCurrentPositionAsync({ enableHighAccuracy: true });
return position;
// return new Promise<Position>((resolve, reject) => {
// navigator.geolocation.getCurrentPosition(position => resolve(position), e => reject(e));
// });
};
Position never resolves when centerLocation is called from componentDidMount. However, when I call it again via the button, it works fine.
centerLocation = () => {
return getCurrentLocation().then((position) => {
console.log(position);
if (position) {
this.setState({
region: {
latitude: position.coords.latitude,
longitude: position.coords.longitude,
latitudeDelta: 0.003,
longitudeDelta: 0.003,
},
});
}
});
}
I’m having similar issues for other screens/usages with the location as well. For example, I have a feed that tries to pull content from a certain location, but the initial location call never resolves, and only works once I manually refresh the feed. Only on android though. Thanks so much!