有没有办法来限制谷歌的地方自动完成搜索一个城市的街道?(Is there a way to rest

2019-07-29 11:25发布

使用谷歌的地方时,我可以将搜索限制到一个城市的街头Autocomplete

Answer 1:

您可以设置Autocomplete选项使用geocode的类型,这将限制返回到地址的结果:

var input = document.getElementById( 'searchTextField' );
var options = {
  bounds: yourBounds,
  types: ['geocode']
};

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

有没有办法限制的结果, 只是街道,但是这将在很大程度上实现你想要的。 如果设置yourBounds正确,也许仅限于城市地区,这将让你尽可能接近目前可能。

更新回复评论:

如果你想进入一个城市开始,你可以在地图的边界绑定到Autocomplete的界限,这将对地图自动设置它的视口时,在选择一个城市Autocomplete

var options = {
  bounds: yourBounds,
  types: ['(cities)']
};
autocomplete =
    new google.maps.places.Autocomplete( input, options );
// This will bind the map's bounds to the Autocomplete's bounds:
autocomplete.bindTo('bounds', map);

然后,您可以更新Autocomplete的类型,类似于上面,使其返回地址:

autocomplete.setTypes( [ "geocode" ] );

从那里,你可以有一个读商家信息库的API文档 。



Answer 2:

HTML代码只是一个例子(Twitter的引导3兼容:))

<div class="well container">
    <form role="form">
      <div class="form-group">
        <label for="locality" class="sr-only">Stadt oder PLZ</label>
          <input type="text" class="form-control" id="locality" name="locality" placeholder="Stadt oder PLZ" />
          <p class="help-block">Bitte zuerst die Stadt oder PLZ eingeben.</p>
      </div>
      <div class="form-group">
        <label for="route" class="sr-only">Straße / Hausnummer</label>
        <div class="row">
          <div class="col-xs-6">
            <input type="text" class="form-control col-xs-8" id="route" name="route" placeholder="Straße" disabled="true" />
          </div>
          <div class="col-xs-6">
            <input type="text" class="form-control col-xs-4" id="street_number" name="street_number" placeholder="Nr." disabled="true" />
          </div>
        </div>
      </div>
    </form>
</div>

我认为的代码是不言自明的,只是试试

<script>
var localityInput = document.getElementById('locality');
var localityOptions = {
  types: ['geocode'], //['(cities)'], geocode to allow postal_code if you only want to allow cities then change this attribute
  componentRestrictions: {country: 'de'}
};
autocomplete = new google.maps.places.Autocomplete(localityInput, localityOptions);

google.maps.event.addListener(autocomplete, 'place_changed', function() {
    $('#route').attr('disabled', false).focus();
    $('#street_number').attr('disabled', false);

    var boundsByCity = autocomplete.getPlace().geometry.viewport;
    var routeInput = document.getElementById('route');
    var routeOptions = {
        bounds: boundsByCity,
        types: ['geocode']
    };
    new google.maps.places.Autocomplete(routeInput, routeOptions);
});
</script>

DEMO: 的jsfiddle

另一个演示更先进



Answer 3:

设置类型的区域

var input = document.getElementById( 'searchTextField' );

var options = {
  bounds: yourBounds,
  types: ['(regions)']
};

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

希望能帮助到你..



Answer 4:

我一直在寻找一种方式来限制地方自动填充到street_number水平,但一个也没找到。

我可以拿出最好的解决方法是检查该street_number的地方结果成分和呈现通知用户,如果它不见了。

示例代码:

var searchbox = document.getElementById('location');

var options = {             
    types: ['geocode']
};

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

google.maps.event.addListener(autocomplete, 'place_changed', function() {
        var place = autocomplete.getPlace();

        var foundStreet = false;

        for (var i = 0; i < place.address_components.length; i++) {
            var c = place.address_components[i];

            for (var j = 0; j < c.types.length; j++) 
            {
                if (c.types[j] == "street_number")
                {
                    foundStreet = true;
                    break;
                }
            }

            if (foundStreet)
                break;
        }

        if (!foundStreet)
            alert("Not a valid street number, add some pretty message to the user.");

        console.log(place);
    });


文章来源: Is there a way to restrict the Google Places Autocomplete to search a city's streets?