【问题标题】:Get only countries to autocomplete from Google Maps API仅从 Google Maps API 获取要自动完成的国家/地区
【发布时间】:2012-10-16 15:41:21
【问题描述】:

我正在使用 Google Maps API 来获取城市和国家/地区的自动完成列表(没有其他详细信息),而且效果很好。

var input = document.getElementById('newAddress');
    var options = {
        types: ['(cities)']
    };

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

现在我想做完全相同的事情,但只获取国家/地区名称。就像用 types: ['(countries)'] 替换 types: ['(cities)']...
(我试过但没用)

我应该怎么做才能只让国家进入我的自动完成功能?

【问题讨论】:

    标签: javascript google-maps google-maps-api-3


    【解决方案1】:

    我一直在使用 Google Autocomplete API,这是我能找到的将您的结果仅限于国家/地区的最佳解决方案:

    var autocomplete = new google.maps.places.Autocomplete(input, options);
    var result = autocomplete.getPlace();
    console.log(result); // take a look at this result object
    console.log(result.address_components); // a result has multiple address components
    
    for(var i = 0; i < result.address_components.length; i += 1) {
      var addressObj = result.address_components[i];
      for(var j = 0; j < addressObj.types.length; j += 1) {
        if (addressObj.types[j] === 'country') {
          console.log(addressObj.types[j]); // confirm that this is 'country'
          console.log(addressObj.long_name); // confirm that this is the country name
        }
      }
    }
    

    如果您查看返回的结果对象,您会看到有一个 address_components 数组,其中包含代表地址不同部分的多个对象。在每个对象中,它将包含一个“types”数组,在这个“types”数组中,您将看到与地址相关的不同标签,包括国家标签。

    【讨论】:

    • 不客气 - 很高兴这对您有帮助 @TareqJobayere
    • 非常感谢 :)
    • 当您搜索一个城市时,您会获得与该城市相关的多条记录,例如 i.imgur.com/HopVQj3.png 。有没有办法只获取城市名称(单条记录而不是多条记录)?
    • @Bangash 你对此有什么建议?你明白了吗?
    【解决方案2】:

    没有快速的解决方案,因为 Google 只提供两种类型的集合:['(cities)'] 和 ['(regions)']

    没有可用的 ['(国家)']。

    此处的文档:https://developers.google.com/places/documentation/autocomplete#place_types

    编辑:

    您也可以使用来自以下网址的自动完成插件:http://www.geognos.com/api/en/countries/info/all.json

    【讨论】:

    • 那么完全没有其他方法可以只获取国家/地区吗?
    • 如果您限制自己使用 Google Maps API 自动完成功能,则不会
    【解决方案3】:

    这是我使用 python sdk 完成的,但它应该很容易翻译成任何其他 sdk,包括 JS:

    import googlemaps
    
    
    def country_search():
        gmaps = googlemaps.Client(key='Your-Key')
    
        # Country code is ISO standard, so it is easy to get country_name based on the code
        country_name, country_code = 'Bahamas', 'BS'
        
        # This limits the search for very few results
        result = gmaps.places_autocomplete(input_text=country_name, types='(regions)', components={'country': [country_code]})
        
        # There should be only one item that has type 'country' in `result`
        country = next(filter(lambda info: any(type == 'country' for type in info['types']), result))
    
        print(country["place_id"])
    

    【讨论】:

      【解决方案4】:
      <!DOCTYPE html>
      <html>
      <head>
      <link type="text/css" rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500">
      <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=places"></script>
      </head>
      <body>
      <div id="locationField">
      <input id="autocomplete" placeholder="Enter your address" type="text"></input>
      </div>
      <div><pre><p id="data"></p></pre></div>
      <script type=">
      var autocomplete;
      function initialize() {
      autocomplete = new google.maps.places.Autocomplete(document.getElementById('autocomplete'),{ types: ['(cities)'] });
      google.maps.event.addListener(autocomplete, 'place_changed', function() {
      var address_components=autocomplete.getPlace().address_components;
      var city='';
      var country='';
      for(var j =0 ;j<address_components.length;j++)
      {
       city =address_components[0].long_name;
       if(address_components[j].types[0]=='country')
      {
          country=address_components[j].long_name;
      }
      }
      document.getElementById('data').innerHTML="City Name : <b>" + city + "</b> <br/>Country Name : <b>" + country + "</b>";
      });
      }
      initialize();
      </script>
      </body>
      </html>
      

      【讨论】:

        猜你喜欢
        • 2017-10-20
        • 1970-01-01
        • 2012-11-21
        • 2021-08-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-16
        • 2015-03-09
        相关资源
        最近更新 更多