Created
October 12, 2017 22:00
-
-
Save lucianomlima/6b2bb7fca0cf0b69c397ef695ca97a24 to your computer and use it in GitHub Desktop.
React Native Geocoder example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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