Google AutocompleteService and Autocomplete return different results with the same query

I have the following query, which is a zip code: 11368 .

When I create an autocomplete object (not a service), I get the actual zip code region at the top:

enter image description here

This is the code:

  autocomplete = new google.maps.places.Autocomplete( document.getElementById('PlaceSearch'), { types: ['(regions)'] }); [...] 

This is exactly what I want. However, I need to use a customizable autocomplete system for design reasons, so I switched to AutocompleteService , which is a non-interface, only for the code of the same thing (well, at least it should be). I create my autocomplete service:

x = new google.maps.places.AutocompleteService();

And I use:

  x.getQueryPredictions({ input: key }, function (results, status) { if (status == google.maps.places.PlacesServiceStatus.OK) { [...] }); }); 

However, this is what I get from the results:

enter image description here

I use the description property to display the record, although this is not a problem, since the ids of the first record in the returned places are also completely different, although I type the exact same request.

I also tried:

x.getQueryPredictions({ input: key, types: ['(regions)'] } ,

x.getQueryPredictions({ input: key, types: ['(geocode)'] } ,

x.getQueryPredictions({ input: key, types: ['geocode'] }

However, this has no effect. This is exactly the same. I also saw a Different result between Google Autocomplete and AutocompleteService , but it answers the question at the typeahead level, while the results on my request are incorrect at the API level without a hit on the type; I tested it with a debugger in the callback function directly from the autocomplete service.

Then I checked network requests in both cases, and here is the request when working on:

https://maps.googleapis.com/maps/api/place/js/AutocompletionService.GetPredictions?1s11368&4sen-US&9s(regions)&15e3&key=XXXXXX&callback=_xdc_._qxy1y2&token=45006

It returns the correct JSON.

Here is a query that returns invalid JSON:

https://maps.googleapis.com/maps/api/place/js/AutocompletionService.GetQueryPredictionsJson?1s11368&4sen-US&9s(regions)&15e3&key=XXXXXX&callback=_xdc_._38p24d&token=20471

And it returns the wrong JSON. From what I see, it is clear that I need to pass (regions) as the types parameter, and this is still incorrect. More absurd, the only difference between the two requests is a variable named token (I tried replacing the wrong request token with the correct one, but I have the error The Google Maps JavaScript API must be downloaded directly from Google servers. ) And the callback function, which I of course, has nothing to do with the returned JSON. The only thing I can think of is that Google does something at the initialization level on its servers, returning the token tied to this server / instance (hence the token).

What am I doing wrong, and how can I get the same results using Google’s own typeahead program?

+7
javascript autocomplete google-places-api
source share
1 answer

I have found a solution. I called getQueryPredictions instead of getPredictions and returned different results. I changed my code to getPredictions and it started working correctly.

+2
source share

All Articles