Skip to content

Instantly share code, notes, and snippets.

@lucianomlima
Created October 12, 2017 22:00
Show Gist options
  • Save lucianomlima/6b2bb7fca0cf0b69c397ef695ca97a24 to your computer and use it in GitHub Desktop.
Save lucianomlima/6b2bb7fca0cf0b69c397ef695ca97a24 to your computer and use it in GitHub Desktop.
React Native Geocoder example
import React from 'react';
import { View, Text } from 'react-native';
import Geocoder from 'react-native-geocoder';
// Chave do Google Maps
Geocoder.apiKey = 'AIza...';
export default class Example extends React.Component {
constructor(props) {
super(props);
this.state = {
place: 'Localizando endereço...'
}
}
componentWillMount() {
// Coordenadas obtidas de geolocalização ou API
if (this.props.coordinate !== null) {
const location = {
lat: this.props.coordinate.latitude,
lng: this.props.coordinate.longitude
};
Geocoder.geocodePosition(location)
.then(res => {
// res é um array com todas as possibilidades sendo a primeira a mais próxima
this.setState({
place: res[0]
});
})
.catch(err => {
// Tratar erro aqui
});
}
}
render() {
return (
<View>
<Text>{this.state.place.toString()}</Text>
</View>
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment