Reputation: 119
I'm creating an ecommerce app and we need the postal code to calculate shipping costs, using address autocomplete from google place api, not sure if it's a limitation from the api, but it doesn't return a postal code. I'm using https://github.com/FaridSafi/react-native-google-places-autocomplete for the autocomplete.
I looked through google docs as well, and it doesn't mention anything about it.
Just wondering if there's a way to get a postal code from an address?
Upvotes: 1
Views: 3187
Reputation: 1989
The Google autocomplete API does not return a postal code as part of its results.
The package you mentioned has a fetchDetails
props, which will call the Google Places API to get more information including the postal code. You can then access the data from details
in onPress
.
import React from 'react';
import { GooglePlacesAutocomplete } from 'react-native-google-places-autocomplete';
const GooglePlacesInput = () => {
return (
<GooglePlacesAutocomplete
placeholder='Search'
fetchDetails={true}
onPress={(data, details) => {
console.log('data: ', data);
console.log('details: ', details);
}}
query={{
key: 'YOUR API KEY',
language: 'en',
}}
/>
);
};
export default GooglePlacesInput;
Upvotes: 2
Reputation: 311
It works for me when I iterate through the array of objects (address_components) inside details, as you can see here:
https://stackoverflow.com/a/68553851/14608654
Upvotes: 2