Kat*_* L. 3 google-maps autocomplete search-box angularjs
我正在使用Angular Google Maps来获取带有搜索框的地图.
我试图将搜索限制为"地址"和国家/地区(法国),因此我想使用搜索框指令的"自动填充"选项,如文档中所述.
问题是autocomplete:true我的代码中的位确实有效地限制了搜索,但它阻止了事件的触发(地图和标记不会更新).但是控制台中没有错误.
但是,如果我注释掉或删除autocomplete:true,则事件触发(地图和标记被刷新),但搜索不限于法国和地址......
这是我的代码:
var events = {
places_changed:function (searchBox) {
var place = searchBox.getPlaces();
if (!place || place == 'undefined' || place.length == 0) {
console.log('no place data :(');
return;
}
// refresh the map
$scope.map = {
center:{
latitude:place[0].geometry.location.lat(),
longitude:place[0].geometry.location.lng()
},
zoom:10
};
// refresh the marker
$scope.marker = {
id:0,
options:{ draggable:false },
coords:{
latitude:place[0].geometry.location.lat(),
longitude:place[0].geometry.location.lng()
}
};
}
};
$scope.searchbox = {
template:'searchbox.tpl.html',
events:events,
options:{
autocomplete:true,
types:['address'],
componentRestrictions:{
country:'fr'
}
}
};
Run Code Online (Sandbox Code Playgroud)
并在html文件中:
<div style="height: 500px;">
<script type="text/ng-template" id="searchbox.tpl.html">
<input type="text"
placeholder="{{'searchbox.google.maps' | text}}"
style="width:270px; height:30px;">
</script>
<ui-gmap-google-map center='map.center' zoom='map.zoom'>
<!--SEARCHBOX-->
<ui-gmap-search-box template="searchbox.template"
events="searchbox.events"
position="BOTTOM_RIGHT"
options="searchbox.options"></ui-gmap-search-box>
<!--MARKER-->
<ui-gmap-marker coords="marker.coords"
options="marker.options"
events="marker.events"
idkey="marker.id">
</ui-gmap-marker>
</ui-gmap-google-map>
</div>
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么 ?有没有办法获得对搜索和事件的限制?
先感谢您 !
好吧,我弄清楚了,所以我回答了我自己的问题,万一其他人被卡住了!
如果autocomplete:true,那么它的不places_changed和searchBox.getPlaces(),但它是place_changed和searchBox.getPlace().而且我们没有得到一系列的地方,只有一个地方.
这是一个有效的代码:
var events = {
place_changed:function (searchBox) {
var place = searchBox.getPlace();
if (!place || place == 'undefined') {
console.log('no place data :(');
return;
}
// refresh the map
$scope.map = {
center:{
latitude:place.geometry.location.lat(),
longitude:place.geometry.location.lng()
},
zoom:10
};
// refresh the marker
$scope.marker = {
id:0,
options:{ draggable:false },
coords:{
latitude:place.geometry.location.lat(),
longitude:place.geometry.location.lng()
}
};
}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2544 次 |
| 最近记录: |