Problems returning the address through geolocation

Asked

Viewed 213 times

2

I’m trying to use the library React-Native-geocoder to return the address through the latitude and longitude of the device.

In response to another question and some more research, I came to this code:

import React, { Component } from 'react';
import { 
  AppRegistry, 
  View, 
  Text
} from 'react-native';
import Geocoder from 'react-native-geocoder'; // 0.5.0

Geocoder.apiKey = 'AIzaSyDtQ0zsYr1c_V7UmlHFekeFIGM2nDwnDEA';

export default class testeGeocoder extends Component {
  constructor(props) {
    super(props);

    this.state = {
      latitude: null,
      longitude: null,
      place: 'Localizando endereço...',
      error: null,
    };
  }

  componentDidMount() {
    navigator.geolocation.getCurrentPosition(
      (position) => {
        this.setState({
          latitude: position.coords.latitude,
          longitude: position.coords.longitude,
          error: null,
        });
      },
      (error) => this.setState({ error: error.message }),
      { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 },
    );

    Geocoder.geocodePosition({ lat: this.state.latitude, long: this.state.longitude})
      .then(res => {
          this.setState({
              place: res[0].formatedAddress
          })
          .catch((error) => {
            this.setState({ error: error.message })
          });
      });
  }

  render() {
    return (
      <View style={{ flexGrow: 1, alignItems: 'center', justifyContent: 'center' }}>
        <Text>Latitude: {this.state.latitude}</Text>
        <Text>Longitude: {this.state.longitude}</Text>
        <Text>{this.state.place.toString()}</Text>
        {this.state.error ? <Text>Error: {this.state.error}</Text> : null}
      </View>
    );
  }
}

AppRegistry.registerComponent('testeGeocoder', () => testeGeocoder);

However this returns to me the correct latitude and longitude, but is Localizando endereço... and never returns.

Where am I going wrong?

  • first thing, the position is being returned correctly? if yes tries to put a catch in the call for geocodePosition by Menis so that you will have a better explanation of the error

  • I’ve been tracking some git issues of them, apparently it’s their mistake even so much that I abandoned and used the direct call to the google api

1 answer

1

The problem is in your componentDidMount, the getCurrentPosition is an asynchronous function, so when you call the Geocoder.geocodePosition in their state latitude and longitude are still null. Try to put the Geocoder.geocodePosition within the getCurrentPosition as below:

componentDidMount() {
    navigator.geolocation.getCurrentPosition(
      (position) => {
        this.setState({
          latitude: position.coords.latitude,
          longitude: position.coords.longitude,
          error: null,
        });
         Geocoder.geocodePosition( { lat: position.coords.latitude, lng: position.coords.longitude})
          .then(res => {
              this.setState({
                  place: res[0].formatedAddress
              });
          });
      },
      (error) => this.setState({ error: error.message }),
      { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 },
    );

  }
  • Tested here: https://snack.expo.io/Hywmw03TZ but still no return address

  • Sorry, my mistake. I edited the call to Geocoder.geocodePosition( position.coords.latitude, position.coords.longitude)

  • Important question, are you using Build with native code or create-React-Nactivate-app from React-Native? Geocoder has native dependencies that need to be 'linked' in the project.

  • I created the project with the react-native init added the <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> at the AndroidManifest.xml and used the react-native link to 'link' the project

  • I have already worked on the project and I am currently using this code: https://snack.expo.io/r18t-A3pb

  • But I haven’t been able to return the address yet

  • I made some modifications to your code leaving thus. A question, you have already tried to pass a fixed location on the geocoder to verify that the problem is not in the lib setting?

  • I tried yes and get the same error I’m getting now Cannot read property 'geocodePosition' of undefined I even opened a issue in the lib repository to see if I can get some help there: https://github.com/devfd/react-native-geocoder/issues/62

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.