Google GeoCode自动填充限制状态

我正在尝试在表单中添加Google Geocode自动填充字段。

我已经能够限制返回到国家(AU)的结果,但如果我可以将结果限制到州,那就更好了。 (新南威尔士/新南威尔士州)

这是我当前的initialize()函数:

 function initialize() { var options = { types: ['geocode'], componentRestrictions: {country:"AU"} }; autocomplete = new google.maps.places.Autocomplete( (document.getElementById('autocomplete')),options); google.maps.event.addListener(autocomplete, 'place_changed', function() { fillInAddress(); }); } 

我已阅读文档,发现您可以使用types参数来限制结果,但我无法弄清楚确切的代码应该是什么。

只需将其更改为:

 types: ['administrative_area1':'NSW'], // OR 'New South Wales' 

和以上类似的各种组合(=代替:等)没有奏效..

有人能指出我正确的方向,实际的语法是我想要实现的目标吗?

谢谢。

我使用autocomplete而没有任何类型的filter,但在发送查询之前自动将我感兴趣的状态附加到所有用户输入。

例如,对于想要获得加州结果的人来说,如果他们要搜索Sacramento它会自动更正Sacramento, California的输入,这对我来说有点好处。 您可以更加具体地了解您发送的数据,例如Sacramento, California, USA

在应用中保留状态纬度/长度边界列表,并使用它们限制自动完成。

  var stateBounds={ ca: ["32.812736", "-119.216815", "34.608128", "-117.039301"] // lat/long boundaries list for states/provinces. }; function getStateBounds(state) { return new google.maps.LatLngBounds( new google.maps.LatLng(stateBounds[state][0], stateBounds[state][1]), new google.maps.LatLng(stateBounds[state][2], stateBounds[state][3]) ); } new google.maps.places.Autocomplete(document.getElementById('search'), { types: ['address'], componentRestrictions: {country: 'us'}, bounds: getStateBounds('ca') // get LatLngBounds for ca. });