javascript 使用 Google Maps API 自动完成国家列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21669015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 21:26:15  来源:igfitidea点击:

Autocomplete Countries List using Google Maps API

javascriptgoogle-mapsgoogle-places

提问by Mahahari

I have the following code which I've been manipulating for a while, but I simply can't get it to return results..

我有以下代码,我已经操纵了一段时间,但我根本无法让它返回结果..

    <script src="http://maps.googleapis.com/maps/api/js?sensor=false&amp;libraries=places" type="text/javascript"></script>
    <script type="text/javascript">
        function initialize() {
            var options = {
                types: ['(cities)'],
                componentRestrictions: {country: "us"}
            };
            var input = document.getElementById('searchTextField');
            var autocomplete = new google.maps.places.Autocomplete(input , options);
        }
        google.maps.event.addDomListener(window, 'load', initialize);
    </script>

</head>
<body>
    <div class = "row">
        <input id="searchTextField" type="text" size="50" placeholder="Enter a location" autocomplete="on">
    </div>
</body>

Using the above code i am able to get the cities list for a specific Country. But my requirement is I need to get the Countries list alone.

使用上面的代码,我可以获得特定国家/地区的城市列表。但我的要求是我需要单独获得国家名单。

If the options is not passed then all the Places are returned as Result.

如果未传递选项,则所有位置都将作为结果返回。

Is there any way to filter the countries alone ??? Please help me if you have any solution for this.

有没有办法单独过滤国家?如果您对此有任何解决方案,请帮助我。

回答by Will

At this point, you can't as there is no 'country' place type. Regions will autocomplete countries though (but not countries alone).

在这一点上,你不能,因为没有“国家”地方类型。地区将自动完成国家(但不仅仅是国家)。

    var options = {
        types: ['(regions)']
    };

Get only countries to autocomplete from Google Maps APIoffers some alternative solutions. Here's an example of regions alone: http://plnkr.co/edit/QjPEczt6AosghVP8EHNV

仅从 Google Maps API 获取国家/地区自动完成功能提供了一些替代解决方案。这是单独区域的示例:http: //plnkr.co/edit/QjPEczt6AosghVP8EHNV