Created
June 18, 2019 03:30
-
-
Save joeljerushan/43782c172d9bf5690cdbc5362b8fc0c5 to your computer and use it in GitHub Desktop.
GooglePlacesAutocomplete, google map api autocomplete address_components into one object - react native way
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
getPlaceInfo() { | |
//setting address_components from GooglePlacesAutocomplete Return | |
let place = this.state.address_components | |
//creating object for namespaces from GooglePlacesAutocomplete | |
var place_info = { | |
street_number: 'short_name', | |
route: 'long_name', | |
locality: 'long_name', | |
administrative_area_level_1: 'short_name', | |
country: 'long_name', | |
postal_code: 'short_name', | |
administrative_area_level_2: 'short_name', | |
neighborhood: 'long_name', | |
}; | |
//create readable object | |
var address_list = {} | |
//the loop | |
for(let i = 0; i < place.length; i++) { | |
//getting namespaces from GooglePlacesAutocomplete return data | |
var addressType = place[i].types[0]; | |
//matching namespaces from local namespace object(place_info) | |
if (place_info[addressType]) { | |
//add and find GooglePlacesAutocomplete return namespace to a var | |
var val = place[i][place_info[addressType]]; | |
//add key and value to object | |
address_list[addressType] = val | |
} | |
} | |
//whatever you want do it here with that object ;) | |
this.setState({ address_readable: address_list }) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment