Google Places API类型功能..

时间:2011-11-18 21:05:11

标签: google-places-api

<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”显示为当我不希望在结果中返回所有内容时使用的类型(商家等)。我没有结果。

基本上,我想要实现的是搜索一个城市(IE:多伦多)并且只看到如下结果:“Toronto,ON,Canada”。也许我对如何实现此API感到困惑。

非常感谢您的回复!

5 个答案:

答案 0 :(得分:5)

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

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

答案 1 :(得分:3)

您还可以使用国家/地区限制

示例:

<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!">

现在,您可以轻松添加包含城市选择的下拉菜单,并在更改下拉列表时重新过滤城市:)

答案 2 :(得分:1)

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

答案 3 :(得分:0)

请尝试查看jsfiddle

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

类型,可以是建立地理编码,代表   企业或地址。如果未指定类型,则两者都是   返回类型。

答案 4 :(得分: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)