AutocompleteService: componentRestrictions doesn't seem to be functional

661 views
Skip to first unread message

gotofritz

unread,
Aug 8, 2015, 9:15:34 PM8/8/15
to Google Maps JavaScript API v3
Hi. A simple AutocompleteService request.

Loading this API:

And running this script (sorry, tried to do a JSBin http://jsbin.com/sujatuzaro/edit?html,js,output  but it won't load the API)

function runQuery(country) {
  var request = {
    input: 'london',
    componentRestrictions: {country: country},
  };

  var callback = function(predictions, status) {
    if (status != google.maps.places.PlacesServiceStatus.OK) {
      alert(status);
      return;
    }
    var resultsString = predictions.reduce(function (prev, current) {
      return prev + "\n" + current.description;
    }, "RESULTS FOR " + country + "\n--------------\n");
    console.log(resultsString);
  };

  var service = new window.google.maps.places.AutocompleteService();
  service.getQueryPredictions(request, callback);
}

function onAPILoaded() {
  runQuery('uk');
  runQuery('ca');
}

I get the following output:

RESULTS FOR uk
--------------

London, United Kingdom
London, ON, Canada
London Borough of Hillingdon, United Kingdom
London Eye, London, United Kingdom
London Bridge, London, United Kingdom

RESULTS FOR ca
--------------

London, United Kingdom
London, ON, Canada
London Borough of Hillingdon, United Kingdom
London Eye, London, United Kingdom
London Bridge, London, United Kingdom


RESULTS FOR uk
--------------

London, United Kingdom
London, ON, Canada
London Borough of Hillingdon, United Kingdom
London Eye, London, United Kingdom
London Bridge, London, United Kingdom

....

The problem is not that they are repeated a few times (although that is odd) but the fact the componentRestrictions doesn't seem to have any effect.
Any idea? Thanks in advance.

James McGill

unread,
Aug 11, 2015, 3:02:48 AM8/11/15
to google-map...@googlegroups.com
Heya :)

This group is used primarily for announcements now; for questions like this I recommend Stack Overflow instead. There's a maps-api-v3 and places-api tag which will bring this to the attention of the best people.

In the meantime, try using getPlacePredictions rather than getQueryPredictions. This appears to apply the restriction correctly. 

Cheers,
James

--
You received this message because you are subscribed to the Google Groups "Google Maps JavaScript API v3" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-maps-js-a...@googlegroups.com.
To post to this group, send email to google-map...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-maps-js-api-v3.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages