0

Im trying to render conditional component. I have MapView with the location, what i want to achieve is: If the location is granted from the user or the location can not be found, instead of MapView I want to render a Textinput where users can type their location manually. This is my code

export default class GpsLocation extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      region: null,
    };
    this._getLocationAsync();
  }

  _getLocationAsync = async () => {
    let { status } = await Permissions.askAsync(Permissions.LOCATION);
    let location = await Location.getCurrentPositionAsync({
      enableHighAccuracy: true,
    });

    let region = {
      latitude: location.coords.latitude,
      longitude: location.coords.longitude,
      latitudeDelta: 0.0045,
      longitudeDelta: 0.0045,
    };
    this.setState({ region: region });
  };

  render() {
    if (this.status === "granted") {
      return (
        <View style={styles.locationContainer}>
        <Text style={styles.note}>Adresa</Text>
        <MapView
          initialRegion={this.state.region}
          showUserLocation={true}
          showCompass={true}
          rotateEnabled={false}
          style={{ flex: 1, borderRadius: 10 }}
        ></MapView>
      </View>
      );
    } else {
      return (
        <TextInput/>
      );
    }
  }
}
1
  • Please explain the issue that you are facing? Commented Nov 19, 2020 at 17:52

1 Answer 1

2

Try this way

constructor(props) {
    super(props);
    this.state = {
      region: null,
      status : undefined, // add initially `undefined`
    };
    this._getLocationAsync();
  }

_getLocationAsync = async () => {
    let { status } = await Permissions.askAsync(Permissions.LOCATION);
    .......
    this.setState({ ... , status }); // update status here
  };

render() {
  // change here also
  return this.state.status === "granted" ? (
        <View style={styles.locationContainer}>
          <Text style={styles.note}>Adresa</Text>
          <MapView
            initialRegion={this.state.region}
            showUserLocation={true}
            showCompass={true}
            rotateEnabled={false}
            style={{ flex: 1, borderRadius: 10 }}
          ></MapView>
      </View>
      ) : <TextInput/> ;       
  }
Sign up to request clarification or add additional context in comments.

6 Comments

this.status is not recognized and its not connecting to the let { status } = await Permissions.askAsync(Permissions.LOCATION);
Sorry i tested it and is not working this.status === "granted" should be something else because the status is not recognized, can u help me
Sill the same is only showing the TextInput, not the MapView when im giving permissions for location
Change this.status to this.state.status then try. I have updated also
pastebin.com/eMkbwBex here is the code updated, but the same its not functional
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.