Google Places Autocomplete 'address' types also allows street only addresses - google-places-api

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.

Related

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

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.

Google Places API setFields AND getDetails

The google places api docs clearly state: "IMPORTANT: To avoid paying for data that you don't need, be sure to use Autocomplete.setFields() to specify only the place data that you will use." But then when I'm calling the getDetails() method (see in generic-y form below), I'm specifying my fields, which is what setFields() sets.
I THINK I'm basically setting them via the getDetails() method, but given the caution exercised in the docs, I also don't want to surprise anyone with extra costs besides what we need. I've tried to find a clear answer in the docs, but I haven't found one. I'm hoping someone here knows from experience or has better luck with the docs. Thanks!
const placesService = new google.maps.places.PlacesService(myField);
placesService.getDetails(
{
fields: ['address_components'],
placeId: result.place_id,
sessionToken: token,
},
details => this.updateMethod(details.address_components),
);
Autocomplete.setFields is used when implementing Places Autocomplete Widget which will constrain the succeeding Places Details request to specific fields only when a user selects a place from the suggestions. Check out the example here.
However, as for your given sample code, I can see that you are directly using Places Service to get the details of a place given a Place ID and not from the Autocomplete Widget suggestions. With this, you can just set the fields in the request parameter of the getDetails(request, callback) method which is what you have correctly done in your code. See this example.
Hope this helps!

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

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

How to get Country for Google Places API nearby search

I'm using Google Places API to get nearby places, but I've noticed that while I get the place names and addresses, the results don't include the Country.
Is there a way to get the country included in the results?
This is the call I'm using:
https://maps.googleapis.com/maps/api/place/nearbysearch/xml?location=<lat,lng>&radius=100&sensor=true&key=<api_key>
Thanks!
The Geocoding API is what you want.
Try
http://maps.googleapis.com/maps/api/geocode/json?address=Calgary,%20Alberta&sensor=false
it'll return fully formatted address info in several ways (split by administrative level, formatted as an address,etc.)
also the place Types blog in google api will help..link is here

Resources