【问题标题】:Google Places API types functionality..Google Places API 类型功能..
【发布时间】:2011-11-18 21:05:10
【问题描述】:
<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 感到困惑。

非常感谢您的回复!

【问题讨论】:

  • 我不认为地域是一种类型///

标签: google-places-api


【解决方案1】:

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

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

【讨论】:

  • 刚刚转到您发布的链接,虽然类型中列出了位置,但如果您查看此链接(code.google.com/apis/maps/documentation/places/…),您会看到它实际上只列出了两个(看起来很奇怪他们会有一个指向类型列表的链接,然后在它的正下方说只有 2 种类型有效,但看起来就是这样) - 所以无论哪种方式,地理编码看起来都是你想要的选项。
  • 地理编码几乎就是我要找的结果。不幸的是,它仍然列出街道名称。例如:如果我搜索“London, UK”,结果会显示“London Street, SomeCity, SomeCountry”。我在哪里可以找到这些类型的解释?我在 Google 的文档中没有看到任何内容。
  • 地标也将返回地理编码...例如机场。
  • 啊,我现在通过您的链接看到,自动完成功能只能使用两种类型。所以我想地理编码就足够了。感谢布拉德的回复。
  • Daveomania_x,如果是这样的话,你应该接受这个作为答案。谢谢!
【解决方案2】:

你也可以使用国家限制

示例:

<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 时重新过滤城市:)

【讨论】:

    【解决方案3】:

    试试看,看看jsfiddle

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

    types,可以是establishmentgeocode,代表 业务或地址,分别。如果未指定类型,则两者 类型被返回。

    【讨论】:

      【解决方案4】:

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

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

      样品请求:

      https://maps.googleapis.com/maps/api/place/autocomplete/json?input=Tor&key=<YOUR_API_KEY_HERE>&types=(regions)
      

      【讨论】:

        【解决方案5】:

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

        【讨论】:

        猜你喜欢
        • 2015-11-09
        • 2013-04-08
        • 1970-01-01
        • 2016-08-15
        • 1970-01-01
        • 1970-01-01
        • 2015-01-07
        • 1970-01-01
        • 2023-01-21
        相关资源
        最近更新 更多