![](/img/trans.png)
[英]Google maps Places API V3 autocomplete - select first option on enter
[英]Google maps Places API V3 autocomplete - restrict only allow to select hotels in canberra
我的新项目需要从Google地图获取地址信息,但我们的服务只能从堪培拉的所有酒店接送。 我的问题是我如何限制自动完成仅选择堪培拉的酒店。
这是我的代码:
var defaultBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(-35.3345571, 149.0334956, 15),
new google.maps.LatLng(-35.1910655, 149.1817188, 12));
var options = {
bounds: defaultBounds,
types: ['establishment'],
keyword:'hotel',
componentRestrictions: {
country: 'au'
}
};
var input = document.getElementById('dropoff_address2Fromgoogle');
var autocomplete = new google.maps.places.Autocomplete(input,options);
google.maps.event.addListener(autocomplete, 'place_changed', function () {
var place = autocomplete.getPlace();
document.getElementById('dropoff_address2Fromgoogle').value = place.name
+ ", " + place.formatted_address
+ ", " + place.address_components[5].short_name;
});
我使用边界来定义堪培拉中的区域,但搜索结果仍会返回其他区域,而Type过滤器只有设置似乎并没有返回更多结果,这正是我所期望的。
以下是示例代码 ,用于搜索所选城市中的酒店。
基本上,示例代码分两个阶段调用Google Maps API:一个阶段搜索城市( onPlaceChanged()
),另一个阶段搜索酒店。 在后面的搜索中,您将在Places对象上调用附近的Search nearbySearch()
,该对象具有城市搜索的bounds
参数和types
值为'lodging'
的types
参数。
问题在于LatLngBounds期望西南和东北角。
代替:
var defaultBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(-35.3345571, 149.0334956, 15),
new google.maps.LatLng(-35.1910655, 149.1817188, 12));
它应该是:
var defaultBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(-35.1910655, 149.0334956),
new google.maps.LatLng(-35.3345571, 149.1817188));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.