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 forgeocodePosition
by Menis so that you will have a better explanation of the error– Neuber Oliveira
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
– Mateus