【问题标题】:how to auto complete based on selected country using google api?如何使用 google api 根据所选国家/地区自动完成?
【发布时间】:2015-03-09 21:01:15
【问题描述】:

可以根据所选国家/地区查找州列表..

这里我附上了一些示例代码

 $("#state").keypress(function(){
            var input = document.getElementById('state');
            //var c1=$("#country").val();
            var c1="us";
           // alert(c1);
            var options = {componentRestrictions: {country: c1}};  
            new google.maps.places.Autocomplete(input, options);
            });

如果我设置国家是“我们”,它可以工作,但不适用于其他国家..

如何根据所选国家/地区查找州?

请任何人帮助我.....

【问题讨论】:

  • 请提供jsFiddle
  • 也许我遗漏了一些东西,但除了美国以外的大多数国家都没有“州”,只是引用。也许我错了。

标签: jquery google-maps


【解决方案1】:

ISO 3166-1 alpha-2 可用于将结果限制为特定组。目前,您可以使用 componentRestrictions 按国家/地区过滤。

国家/地区必须作为两个字符传递,与 ISO 3166-1 Alpha-2 兼容的国家/地区代码。

Officially assigned country codes

阅读@Francois 回答的this 来源

试试这个代码 sn-p,我创建了这个,可能对你有点帮助。

$(document).ready(function () {
    var options = {
        types: ['(cities)'],
        componentRestrictions: {
            country: "us"
        }
    };
    var input = document.getElementById('autocomplete');
    new google.maps.places.Autocomplete(input, options);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://maps.googleapis.com/maps/api/js?libraries=places"></script>
<label for="autocomplete">Please Insert an address:</label>
<br>
<input id="autocomplete" type="text" size="50">

【讨论】:

    猜你喜欢
    • 2017-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-22
    • 1970-01-01
    • 2016-06-02
    • 2011-12-25
    相关资源
    最近更新 更多