使用Google商家信息Autocomplete
时,我可以将搜索范围限制在城市的街道吗?
答案 0 :(得分:6)
您可以将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" ] );
从那里,您可以阅读Places Library API Docs。
答案 1 :(得分:3)
只是html代码的示例(twitter bootstrap 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
答案 2 :(得分:3)
将类型设置为区域。
var input = document.getElementById( 'searchTextField' );
var options = {
bounds: yourBounds,
types: ['(regions)']
};
autocomplete = new google.maps.places.Autocomplete( input, options );
希望有所帮助......
答案 3 :(得分: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);
});