getCurrentPositionAsync() is not working

i used the exact same code in the documentation to get the current location of the device. but it’s not working. I only can see “waiting” on my device screen. (this.state.location is null). How can i fix this.

import React, { Component } from ‘react’;
import { Platform, Text, View, StyleSheet } from ‘react-native’;
import { Constants, Location, Permissions } from ‘expo’;

export default class App extends Component {
state = {
location: null,
errorMessage: null,
};

componentWillMount() {
if (Platform.OS === ‘android’ && !Constants.isDevice) {
this.setState({
errorMessage: ‘Oops, this will not work on Sketch in an Android emulator. Try it on your device!’,
});
} else {
this._getLocationAsync();
}
}

_getLocationAsync = async () => {
let { status } = await Permissions.askAsync(Permissions.LOCATION);
if (status !== ‘granted’) {
this.setState({
errorMessage: ‘Permission to access location was denied’,
});
}

let location = await Location.getCurrentPositionAsync({});
this.setState({ location });

};

render() {
let text = ‘Waiting…’;
if (this.state.errorMessage) {
text = this.state.errorMessage;
} else if (this.state.location) {
text = JSON.stringify(this.state.location);
}

return (
  <View style={styles.container}>
    <Text style={styles.paragraph}>{text}</Text>
  </View>
);

}
}

const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: ‘center’,
justifyContent: ‘center’,
paddingTop: Constants.statusBarHeight,
backgroundColor: ‘#ecf0f1’,
},
paragraph: {
margin: 24,
fontSize: 18,
textAlign: ‘center’,
},
});

Hi @ven0m!

The code you posted works for me, testing on a physical device. Are you using a device or the simulator?

1 Like

i’m using my device. samsung galaxy j5 10

@ven0m,

Could you try this snack and let me know if it works properly?

Thanks so much!

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.