14

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

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

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

现在我想做完全相同的事情,但只获得国家名称。类似于替换types: ['(cities)']types: ['(countries)']...
(我尝试过但没有用)

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

4

4 回答 4

24

我一直在使用 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 数组,其中包含代表地址不同部分的多个对象。在每个对象中,它将包含一个“类型”数组,在这个“类型”数组中,您将看到与地址相关的不同标签,包括国家标签。

于 2014-06-10T15:52:53.130 回答
11

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

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

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

编辑:

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

于 2012-10-16T15:52:05.723 回答
0

这是我使用 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"])
于 2020-11-30T10:44:29.277 回答
-3
<!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-03-10T07:34:52.687 回答