使用 Google 地方信息时,我可以将搜索限制在城市的街道上Autocomplete
吗?
问问题
17477 次
4 回答
6
您可以设置Autocomplete
要用作类型的选项geocode
,这将限制返回到地址的结果:
var input = document.getElementById( 'searchTextField' );
var options = {
bounds: yourBounds,
types: ['geocode']
};
autocomplete = new google.maps.places.Autocomplete( input, options );
没有办法将结果限制在街道上,但这将在很大程度上实现您想要的。如果您设置yourBounds
正确,可能仅限于城市区域,它将让您尽可能接近。
更新回复评论:
如果您想从输入城市开始,您可以将地图的边界绑定到Autocomplete
s 边界,当在 中选择城市时,地图会自动设置其视口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" ] );
从那里,您可以阅读Places Library API Docs。
于 2012-05-18T14:12:37.930 回答
4
只是 html 代码的一个例子(twitter bootstrap 3 compatible :))
<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>
演示:JSFIDDLE
于 2014-11-05T18:18:11.560 回答
3
将类型设置为区域。
var input = document.getElementById( 'searchTextField' );
var options = {
bounds: yourBounds,
types: ['(regions)']
};
autocomplete = new google.maps.places.Autocomplete( input, options );
希望能帮助到你..
于 2015-11-13T14:16:12.000 回答
2
我正在寻找一种方法来将地方自动完成限制为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);
});
于 2013-07-09T14:41:43.943 回答