9
<html>
    <head>
        <title></title>

        <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=places&sensor=true"></script>
        <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
        <script type="text/javascript">
            $(function () {
                var input = document.getElementById('location');
                var options = {                    
                    types: ["locality"]
                };

                autocomplete = new google.maps.places.Autocomplete(input, options);
            });
        </script>        
    </head>
    <body>
        <div>Location: <input type="text" id="location" style="width:400px;" /></div>               
    </body>
</html>

有我的代码来生成我的自动完成位置文本输入。Google 的支持类型列表 ( http://code.google.com/apis/maps/documentation/places/supported_types.html ) 将“locality”显示为当我不希望所有内容都返回时使用的类型结果(企业等)。我没有得到任何结果。

基本上,我想要实现的是搜索一个城市(即:多伦多)并且只看到如下结果:“多伦多,安大略省,加拿大”。也许我对如何实现这个 API 感到困惑。

非常感谢您的回复!

4

5 回答 5

7

我认为您根据文档寻找的选项是“地理编码”(http://code.google.com/apis/maps/documentation/javascript/reference.html#AutocompleteOptions):

var options = {                    
    types: ["geocode"]
};
于 2011-11-18T21:16:03.920 回答
4

你也可以使用国家限制

例子:

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=places&sensor=false"></script>

<script type="text/javascript">
    function initialize() 
    {
      var input = document.getElementById('searchTextField');
      var options = {
          types: ['(cities)'],
          componentRestrictions: {country: "ca"}
      };

      autocomplete = new google.maps.places.Autocomplete(input, options);
    }

    google.maps.event.addDomListener(window, 'load', initialize);
</script>

<input id="searchTextField" type="text" size="50" placeholder="Anything you want!">

现在,您可以轻松地添加包含城市选择的下拉列表并在下拉列表发生 onchange 时重新过滤城市:)

于 2012-04-17T13:06:49.307 回答
0

试试看jsfiddle

 var options = {                    
      types: ['geocode']
 };

类型,可以是机构地理编码,分别代表企业或地址。如果未指定类型,则返回两种类型。

于 2011-11-18T21:16:08.560 回答
0

如果用户Tor在输入字段中键入并且您想要的输出是,Toronto, ON, Canada那么您应该使用types=(regions), 和括号。

我不知道在提出问题时该选项是否存在,但它现在可用。

样品要求:

https://maps.googleapis.com/maps/api/place/autocomplete/json?input=Tor&key=<YOUR_API_KEY_HERE>&types=(regions)
于 2017-04-27T22:01:01.330 回答
0

你可以这样使用 types: ['geocode' || 'establishment' || 'address']

于 2019-03-27T07:54:31.277 回答