只允许各国从Google Maps API自动填充

时间:2012-10-16 15:41:22

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

我正在使用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 个答案:

答案 0 :(得分:22)

我一直在使用谷歌自动完成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'数组中,你会看到与地址相关的不同标签,包括一个国家的标签。

答案 1 :(得分:8)

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

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

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

修改

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

答案 2 :(得分:0)

这是我使用python sdk完成的操作,但应轻松将其转换为包括JS在内的任何其他sdk:

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"])

答案 3 :(得分:-2)

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