最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Google Places Autocomplete - Stack Overflow

programmeradmin2浏览0评论

I am using google.maps.places.Autoplete, I want city to autoplete but restrict the autoplete results to multiple cities, not just one as in the google documentation.

this is what I have

<script src=";amp;libraries=places" type="text/javascript"></script>
<script type="text/javascript">
 function initialize() {

    var input = document.getElementById('searchTextField');

    /* restrict to multiple cities? */
    var options = {
       types: ['(cities)'],
       ponentRestrictions: {country: "us", "fr"}
    };


    var autoplete = new google.maps.places.Autoplete(input, options);
 }
 google.maps.event.addDomListener(window, 'load', initialize);
</script>

<div>
<input id="searchTextField" type="text" size="50" placeholder="Enter a location" autoplete="on">
</div>

I am using google.maps.places.Autoplete, I want city to autoplete but restrict the autoplete results to multiple cities, not just one as in the google documentation.

this is what I have

<script src="http://maps.googleapis./maps/api/js?sensor=false&amp;libraries=places" type="text/javascript"></script>
<script type="text/javascript">
 function initialize() {

    var input = document.getElementById('searchTextField');

    /* restrict to multiple cities? */
    var options = {
       types: ['(cities)'],
       ponentRestrictions: {country: "us", "fr"}
    };


    var autoplete = new google.maps.places.Autoplete(input, options);
 }
 google.maps.event.addDomListener(window, 'load', initialize);
</script>

<div>
<input id="searchTextField" type="text" size="50" placeholder="Enter a location" autoplete="on">
</div>
Share Improve this question asked Apr 16, 2012 at 8:03 FrancoisFrancois 10.7k5 gold badges55 silver badges65 bronze badges 2
  • Ironically, this is the first example that I have found for filtering by country. Thanks! Where did you go for documentation btw? – fatfrog Commented Nov 30, 2014 at 22:06
  • Workaround here: stackoverflow./questions/11290755/… – metamagikum Commented Mar 17, 2016 at 14:55
Add a ment  | 

3 Answers 3

Reset to default 3

Actually the Places API supports only 1 country as restriction. ComponentRestrictions

I am new to google places autoplete but I think the error is here:

ponentRestrictions: {country: "us", "fr"}

Try this:

ponentRestrictions: {country: ["us", "fr"]}

You can only add 1 country for restrictions. https://developers.google./maps/documentation/javascript/reference#ComponentRestrictions

发布评论

评论列表(0)

  1. 暂无评论