Why are some postal codes not provided by place/details (for given place_id) even though they are returned by places/autocomplete - google-places-api

I try to find a postal code for a given place_id. The place_id was provided by the autocomplete service. The prediction of the autocomplete service shows me a postal code. When calling the details service there is no postal_code in the address components list.
I tried to restrict the autocomplete service to only search for results with postal_code as type in the address components list. But I think there is no restriction for only providing results with postal_code elements.
curl -XGET "https://maps.googleapis.com/maps/api/place/autocomplete/json?key=<API-KEY>&input=88400,Biberach"
#Use place_id of first item: ChIJ1Tzt4ZLFm0cRAlQh0MLHBxE
curl -XGET "https://maps.googleapis.com/maps/api/place/details/json?key=<API-KEY>&placeid=ChIJ1Tzt4ZLFm0cRAlQh0MLHBxE&fields=address_component,type"
My expected behavior is to get a postal_code for the result of place/details. But it isn't there.
Can someone explain to me why this is not provided or if there is a good way to restrict the autocomplete call to only provide results with postal code elements in the address components.

If no postal code is returned this could be due to the result spanning over multiple postal codes (e.g. there is no postal code for "Berlin, Germany" since it could be anything from 10xxx to 14xxx).
A simple workaround could be to include a street and a house number while calling the API.

Related

Find Place requests Returns Only One Result

I'm using the Google Places API endpoint "findplacefromtext" and tried a search similar to the example.
https://maps.googleapis.com/maps/api/place/findplacefromtext/json?input=mongolian%20grill&inputtype=textquery&fields=photos,formatted_address,name,opening_hours,rating&locationbias=circle:2000#47.6918452,-122.2226413&key=YOUR_API_KEY
However, when you use this it only ever returns one result. There is a cafe near me that's called "Cream" but when you pass that as the "input" parameter it returns shops that have a category of "Ice Cream". I thought it should only search the name of the business.... If I can't find the place by name does it search the category type as a fall back? When I execute the same search in Google Maps it returns the same data but I get multiple results and I can see the place I am trying to retrieve 3rd on the search result list.
Is it possible to make it return more than one result? The documentation doesn't mention anything about this.
I believe what you need is the Text Search request. The Find Place request is meant for exact addresses.
The Google Places API Text Search Service is a web service that
returns information about a set of places based on a string — for
example "pizza in New York" or "shoe stores near Ottawa" or "123 Main
Street". The service responds with a list of places matching the text
string and any location bias that has been set.
The service is especially useful for making ambiguous address queries
in an automated system, and non-address components of the string may
match businesses as well as addresses. Examples of ambiguous address
queries are incomplete addresses, poorly formatted addresses, or a
request that includes non-address components such as business names.
Taken from https://developers.google.com/maps/documentation/places/web-service/search#TextSearchRequests

Google Places Autocomplete 'address' types also allows street only addresses

This question here basically asks the same question:
Restrict Google Places Autocomplete to return addresses only
But doesn't get the right answer.
If we look at the official documentation examples they have an option to selected address type. However, on it, we can see that it will allow us to selected just streets:
Is there a way to make Places Autocomplete select real addresses only?
Since this question is quite old, the search result is now somehow different but the fact that address type returns street addresses remains the same.
And to clarify things here, the API is working as intended when it returns street addresses.
types=address seems to be quite vague and what you need to do is to be more specific if you don't want street address to be included.
Here's a table for reference on the different types of restrictions for autocomplete from the Place Types Documentation.
Table 3: Type collections supported in Place Autocomplete requests
The supported types are:
geocode instructs the Place Autocomplete service to return only geocoding results, rather than business results. Generally, you use this request to disambiguate results where the location specified may be indeterminate.
address instructs the Place Autocomplete service to return only geocoding results with a precise address. Generally, you use this request when you know the user will be looking for a fully specified address.
establishment instructs the Place Autocomplete service to return only business results.
(regions) type collection instructs the Places service to return any result matching the following types:
locality
sublocality
postal_code
country
administrative_area_level_1
administrative_area_level_2
(cities) type collection instructs the Places service to return results that match locality or administrative_area_level_3.
To further prove this, I tried using the official documentation example for testing.
I tried searching real street address with both types=address and types=geocode and they return street addresses as expected.
With address type:
With geocode type:
Then I tried the types=establishment and it did not return any street addresses.
With establishment type:
You can use any other types value apart from types=establishment like the (cities) and (regions). As long as you are specific in your restrictions, things will be working fine. You can just put some toggle on your app to change restrictions just like on the official docs example so that your end user could freely choose the restrictions. But it still depends on your use case.
Hope this helps.

Missing country component in geocode API result

I want to get place_ids of hierarchical result, following the answer in Hierarchical & translated results of a placeId using Google Places
Everything goes fine at first, but in this request:
https://maps.googleapis.com/maps/api/geocode/json?latlng=-34.63289,116.12236&key=MY_KEY&language=en&result_type=country|administrative_area_level_1|administrative_area_level_2|locality
country component is missing.
How can I fix this?

Where can I find the list of country code values for Google's custom search API?

Looking at the documentation for Google's custom search API, the list of country codes for the "gl" parameter isn't available.
The gl parameter value is a two-letter country code. The gl parameter
boosts search results whose country of origin matches the parameter
value. See the Country Codes page for a list of valid values.
The link found here: https://developers.google.com/custom-search/json-api/v1/reference/cse/list
points to here: https://developers.google.com/custom-search/docs/xml_results#countryCodes
Which doesn't contain any country codes.
Where can I find the list of valid country code values, so I can be sure of the set of valid values?
XML API reference appendices > Country Codes
https://developers.google.com/custom-search/docs/xml_results_appendices#countryCodes

Can I obtain the country name with Google Places API?

On the request for current place I receive a list of places. Every place has an address but that address is a string so I can't get only the country name.
Could someone help me on this matter?
Country is only explicitly available when using the Place Details API, not the Places API. (https://developers.google.com/maps/documentation/places/web-service/details)
You first need to look up a place_id for the specific place you're looking for. You then use your place_id to pull details from the Details API, which includes information like:
Fully formatted address, including country
Address components
Sample reviews
Phone # and website
Operating hours
If you have read the Google Places Autocomplete doc,you must know about API properties.you can use the type as '(region)' and will return you many results (plus country).And if you look inside address_components field (it's array with dynamic length),you will see,that the last item of array is what you need.Here is a great page to read a correct doc and try examples yourself.
http://www.w3docs.com/learn-javascript/places-autocomplete.html

Resources