Google Places API vs. Google Geocode API - google-maps

I have implemented Google Places API with autocomplete and Google Geocoding Api. The problem is that it seems that the results do not work correctly.
Sometimes some of the choosen results from the autocomplete list do not geocode at all, all i get is the status ZERO_RESULTS.
I know that there could be some problems regarding the fact that Google Autocomplete uses also Places, while Google Geocode uses only postal codes, but how could i limit autocomplete to give me only postal codes results.

Place Types
You may restrict results from a Place Autocomplete request to be of a certain type by passing a types parameter. The parameter specifies a type or a type collection, as listed in the supported types below. If nothing is specified, all types are returned. In general only a single type is allowed. The exception is that you can safely mix the geocode and establishment types, but note that this will have the same effect as specifying no types. The supported types are:
geocode instructs the Place Autocomplete service to return only geocoding (address) results. Generally, you use this request to disambiguate results where the location specified may be indeterminate.
establishment instructs the Place Autocomplete service to return only business results.
the (regions) type collection instructs the Places service to return any result matching the following types:
locality
sublocality
postal_code
country
administrative_area1
administrative_area2
the (cities) type collection instructs the Places service to return results that match either locality or administrative_area3.
Hope this helps
https://developers.google.com/places/documentation/autocomplete

There is no way to achieve it via an Autocomplete.
When you type in a autocomplete the API first will request the predictions, but not the details.
This means that at the time when you see the predictions it's still not clear if there are details available for this place.
There are cases when the particular place can't be found. I'm sure when it would be possible for google to exclude the missing places they wouldn't return them at all.
The only workaround I see would be to create the autocomplete on your own by requesting both, predictions and details , before you apply the predictions. But note: each time the user types into the input this will count as 6 requests(assuming 5 predictions will be returned)

Related

Google Place Autocomplete API showing invalid address

We are currently trying the Google places autocomplete api, but it seems as though Google places autocomplete api also returns invalid (non existent) addresses in the search result.
We want to receive only valid / actual addresses when the user types in the address autocomplete, is there a way to do it?
I searched the api docs but couldn't find anything much helpful.
There is a google places validation api but it only validates after given a single address, but we want to show only valid/actual addresses on search autocomplete and also don't want to call validation api for each of the search result items.
Went through official doc and also experimented several stackoverflow answers but those didn't match our requirements.
SO ref-1
This is Working as Intended as of now
I tried reproducing the Autocomplete request using this:
https://maps.googleapis.com/maps/api/place/autocomplete/json?input=332%20Kennedy%20drive,%20Torrington,%20CT&types=address&key=API_KEY
And the results returned has a long Place ID.
According to the Places API documentation:
"Place IDs uniquely identify a place in the Google Places database and on Google Maps."
The documentation also says that the API may return a different place ID in the response. These place ID types include:
Street addresses that do not exist in Google Maps as precise addresses, but are inferred from a range of addresses.
Segments of a long route, where the request also specifies a city or locality.
Intersections.
Places with an address component of type subpremise.
These IDs often take the form of a long string.
This explains why the API still returned a street address for the input =332 Kennedy drive, Torrington, CT using types=address. The use case of autocomplete is to help the users obtain the closest result even if they are requesting an ambiguous query. So the Autocomplete always tries to return the closest result to their input and types. In your case, it returned a street addresses that do not exist in Google Maps as precise addresses, but are inferred from a range of addresses.
There's a public bug on the issue tracker that is somehow related to this. On the comment #5, it says that:
Suggestions are sometimes created for addresses that may not exist, if there are chances that they do.
Retrieving details for such suggestions may result in the suggested address, if it actually exists or can be synthesized from existing data. Otherwise, the result may be a different address, one that actually exists.
Possible workaround
One thing you can do is to use the validation API.
But since you mentioned that you don't want to use it, I tried the Autocomplete request using this:
https://maps.googleapis.com/maps/api/place/autocomplete/json?input=332%20Kennedy%20drive,%20Torrington,%20CT&types=establishment&key=API_KEY
I just changed the types=address to types=establishment and it seems to return legit addresses. I'm not sure if this would fit on your use case, so you can just comment down if it does not.
And if it does not, I found a similar public bug on the issue tracker which says that similar issues like these are currently being worked on by the Google engineers internally. So what I would advise you to do wait for on update from this public bug.
There's no definite timeline on how long this will be fixed, but you can star it to be notified whenever there's an update.
Hope this helps!
== UPDATE ==
I tried playing around with the types and got to a point where I tried putting an array of types from this table. I tried this:
https://maps.googleapis.com/maps/api/place/autocomplete/json?input=332%20Kennedy%20drive,%20Torrington,%20CT&types=street_address|street_number|premise&key=API_KEY
and it did return some real addresses. I changed the types=address to types=street_address|street_number|premise. You could try it out yourself and play around with the types and see if it works for your use case.
for more information about types, here's a link to the documentation.

Question regarding the Google Autocomplete query with ZERO_RESULT status

I implemented some kind of address validation using Google autocomplete query and for some of the search requests I getting ZERO_RESULT, and based on the docs it means :
ZERO_RESULTS indicates that the search was successful but returned no results. This may occur if the search was passed a bounds in a remote location.
My question is about the second part of this statement: This may occur if the search was passed a bounds in a remote location.
I couldn't find any more details about this part, what is that mean?
I'm asking because the address that I used to test the validation and return ZERO_RESULT is a valid address, I can actually find it in google maps.
This may occur if the search was passed a bounds in a remote location means that the area the autocomplete was searching didn't have any results that matched the query.
The bounds would be "the region defined by location and radius".
This would be more likely to occur with strictbounds.

What is the recommended format for search query in Google and Apple maps?

I want to show the direction from my React Native app to the specific address in Google or Apple maps application depending on the Platform. Are there any requirements for address query format so I can get better search results?
I've found following for Google https://developers.google.com/maps/faq#geocoder_queryformat. Any ideas for Apple Maps?
Here what I’ve found in official docs
Google
How should I format my geocoder queries to maximise the number of successful requests?
The geocoder is designed to map street addresses to geographical coordinates. We therefore recommend that you format geocoder requests in accordance with the following guidelines to maximize the likelihood of a successful query:
Specify addresses in accordance with the format used by the national postal service of the country concerned.
Do not specify additional address elements such as business names, unit numbers, floor numbers, or suite numbers that are not included in the address as defined by the postal service of the country concerned. Doing so may result in responses with ZERO_RESULTS.
Use the street number of a premise in preference to the building name where possible.
Use street number addressing in preference to specifying cross streets where possible.
Do not provide ‘hints’ such as nearby landmarks.
https://developers.google.com/maps/faq#geocoder_queryformat
Apple
Use the CLGeocoder class with a dictionary of Address Book information or a simple string to initiate forward-geocoding requests. There is no designated format for string-based requests: Delimiter characters are welcome, but not required, and the geocoder server treats the string as case-insensitive. For example, any of the following strings would yield results:
“Apple Inc”
“1 Infinite Loop”
“1 Infinite Loop, Cupertino, CA USA”
The more information you can provide to the forward geocoder, the better the results returned to you. The geocoder object parses the information you give it and if it finds a match, returns some number of placemark objects. The number of returned placemark objects depends greatly on the specificity of the information you provide. For this reason, providing street, city, province, and country information is much more likely to return a single address than providing only street and city information. The completion handler block you pass to the geocoder should be prepared to handle multiple placemarks, as shown below.
[geocoder geocodeAddressString:#"1 Infinite Loop"
completionHandler:^(NSArray* placemarks, NSError* error){
for (CLPlacemark* aPlacemark in placemarks)
{
// Process the placemark.
}
}];
https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/LocationAwarenessPG/UsingGeocoders/UsingGeocoders.html

Google Maps Geocode - Never returns multiple results

The Google Maps API documents online for their Geocode API states:
Generally, only one entry in the "results" array is returned for
address lookups,though the geocoder may return several results when
address queries are ambiguous.
However if you hit this example for 'Smith Street' (that counts as ambiguous, right?) you only get one result.
https://maps.googleapis.com/maps/api/geocode/json?address=smith%20street&components=country:GB
So if Smith Street isn't ambiguous, what is? Or is the reality that this API only ever returns one result?
The docs are so misleading when it insinuates that you can search for places and get all locations based on a place name or ambiguous address. Actually, they always returned either zero or one result only in all my trials.
Example:
*Searching "KFC" returns zero results!
*Searching "KFC berlin" returns one result!
In summary, if you don't have a precise address, do not use geocoding to retrieve lats and longs period!
Instead, you should use "Place search", explained Here.
Why?
If you check this Google Maps API blog, it says:
The Geocoding API is best for handling unambiguous queries such as complete postal address strings.
Geocoding API is not recommended if your application handles ambiguous or incomplete queries, such as “123 Main St”, or if it handles queries that may contain non-address information such as apartment numbers or business names.
However, the "Place search" using Places API is much smarter at searching Semantic locations and addresses, including businesses and points of interest.
If you want to make the user search for places using GUI, you can use "Places autocomplete", but they also would mark a single location on the map based on the exact choice the user selects from the autocomplete list. You can find a tutorial Here

Localities around a particular latlang

I require list of localities around a particular latitude and longitude through Google Places.
Currently I'm making a call as
https://maps.googleapis.com/maps/api/place/search/json?location=12.96207210,77.71577370&radius=3700&types=political&sensor=false&key=API_KEY
However,it is returning only those places which are approximately 3700 m far from that place.
I require all places returned in the output.
It appears that you are trying to retrieve all locality results close to you, as you are type filtering your request by type political.
Unfortunately the Google Places API is not designed for this and will only returning two political and locality results per request for area identification.
If you think that returning more than two political or locality results per request would be a useful feature for the Google Places API, you can submit a 'Places API - Feature Request' here.