shukerullah / react-geocode

A React module to transform a description of a location (i.e. street address, town name, etc.) into geographic coordinates (i.e. latitude and longitude) and vice versa.
MIT License
207 stars 33 forks source link

Unable to return the address #12

Closed Louventia closed 6 years ago

Louventia commented 6 years ago

Hello! I'm very new to react and I am required to convert latitude to longitude which the latitude and longitude get from firebase. So the problem is that I am able to console log the address. However, I am not being able to display

This is the function that I wrote for the converting

getAddressFromLatLong(lat, long) {
  Geocode.fromLatLng(lat, long).then(
      response => {
          const address = response.results[0].formatted_address;
          console.log(address);
          return address;
      },
      error => {
          console.error(error);
      }
  )
}

This is where I render the address

return (
    <tr key={atData.uid}>
        <td>{atData.uid}</td>
        <td>{atData.user}</td>
        <td><TimeStamp time={atData.dateTime} format='full' /></td>
        <td>{this.getAddressFromLatLong(atData.latitude, atData.longtitude)}</td>
    </tr>
)

Please advise >.<

shukerullah commented 6 years ago
state = {
  address: 'fetching',
}

componentDidMount() {
  this.getAddressFromLatLong(atData.latitude, atData.longtitude);
}

getAddressFromLatLong(lat, long) {
  Geocode.fromLatLng(lat, long).then(
    response => {
      this.setState({
        address: response.results[0].formatted_address
      })
    },
    error => {
      this.setState({
        address: error.message
      })
    }
  );
}

render() {
  return (
    <td>{this.state.address}</td>
  )
}