Reputation: 21
I have a site that validates zip codes. If a user types in a zip code I store the formatted address, lat, long, etc. If they go back in to edit the form the next day, I set the field to the formatted address and revalidate. The problem is not all formatted addresses return the zip the second time - even though I'm passing it in to the query.
-- RETURNS FORMATTED ADDRESS: Woodland, CA 95776, USA https://maps.googleapis.com/maps/api/geocode/json?address=95776&key=
-- DOESN'T RETURN A POSTAL CODE - GIVES APPROXIMATE EVEN THOUGH I'M PASSING IT IN https://maps.googleapis.com/maps/api/geocode/json?address=Woodland, CA 95776, USA&key=
-- RETURNS POSTAL CODE AS EXPECTED, EVEN THOUGH LENEXA, HAS MULTIPLE POSTAL CODES /maps/api/geocode/json?address=Lenexa, KS 66227, USA&key=
Upvotes: 0
Views: 2433
Reputation: 32100
I would suggest storing the place ID instead of the address string. This is recommended by Google:
https://developers.google.com/places/web-service/place-id#save-id
So the first time you execute
https://maps.googleapis.com/maps/api/geocode/json?address=95776&key=YOUR_API_KEY
and store the resulting place ID ChIJARdFFWLRhIARK6RIUxEbJ30
.
The next time you should execute the place ID lookup to get exactly the same result as the original request
https://maps.googleapis.com/maps/api/geocode/json?place_id=ChIJARdFFWLRhIARK6RIUxEbJ30&key=YOUR_API_KEY
Upvotes: 0