Google GeoCode 自动完成限制状态

Jak*_*ake 5 jquery google-maps autocomplete google-maps-api-3 google-geocoder

我正在尝试将 Google Geocode Autocomplete 字段添加到我的表单中。

我已经能够让它限制返回到国家 (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();
  });
}
Run Code Online (Sandbox Code Playgroud)

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

只需将其更改为:

types: ['administrative_area1':'NSW'], // OR 'New South Wales'
Run Code Online (Sandbox Code Playgroud)

并且与上述类似的各种组合(= 而不是:等)都不起作用..

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

谢谢。

Con*_*hol 6

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

    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.
    });
Run Code Online (Sandbox Code Playgroud)


jag*_*oft 4

我使用自动完成功能,没有任何类型的过滤器,但在发送查询之前自动将我感兴趣的状态附加到所有用户输入中。

例如,对于想要加州结果的人来说,如果他们要搜索Sacramento它,就会自动更正输入Sacramento, California,这对我来说效果很好。例如,您可以对发送的数据更加具体Sacramento, California, USA