Reputation: 1082
I am using Google Geocoding API to get lat
, lng
, country
and postal_code
from any address string.
In certain situations I noticed a problem where in my opinion the API is wasteful with requests. Is there a way to work around it or even an intended solution that I don't see?
New York, USA
lat
, lng
) somewhere in the city center, I don't get a postal_code
. I can now reverse geocode using lat
, lng
and get my postal_code
that way, but now I have to make two requestspostal_code
right away?Upvotes: 1
Views: 1536
Reputation: 32198
In example that you provided, request https://maps.googleapis.com/maps/api/geocode/json?address=New%20York%2C%20USA&key=YOUR_API_KEY
returns a feature of type locality (place ID ChIJOwg_06VPwokRYv534QaPC8g).
Note that this feature refers to entire city, not to the position of the center of the city.
As locality is not a single point (it has a polygon shape) and the feature of locality overlaps many features of postal codes, Google doesn't return any value for postal code due to ambiguity. Unfortunately, you cannot change this behavior. The only workaround as you mentioned is reverse geocode a postal code of the central point.
I hope my answer clears up your doubt.
Upvotes: 1