我一直遇到与 this 非常相似的问题
然而,那个人的问题从未得到回答,我的情况略有不同。
我正在尝试向我的 map 添加一个点击事件,该事件会将 map 的中心更改为点击的位置。我执行此操作的代码效果很好,但它只能运行一次,然后当我再次单击时出现此错误:
angular-google-maps.js:6815 未捕获类型错误:无法读取未定义的属性“click”
这是我的 map 对象:
vm.map = {
center: {
latitude: l.latitude,
longitude: l.longitude
},
zoom: 13,
events: {
click: function(map, event, coords) {
vm.map = {
center: {
latitude: coords[0].latLng.lat(),
longitude: coords[0].latLng.lng()
},
};
$scope.apply();
}
}
};
这是我的 html:
<ui-gmap-google-map center="location.map.center" zoom="location.map.zoom" draggable="true" options="tabLocations.map.options" events=location.map.events>
<ui-gmap-search-box template="'scripts/components/tab-locations/searchbox.tpl.html'" events="location.map.searchbox.events" parentdiv="'searchbox-container'"></ui-gmap-search-box>
<ui-gmap-marker ng-if="location.active" idKey="1" coords="location" options="{draggable: false, icon: 'images/icons/gps-marker-active.svg'}"></ui-gmap-marker>
<ui-gmap-marker ng-if="!location.active" idKey="1" coords="location" options="{draggable: false, icon: 'images/icons/gps-marker-inactive.svg'}"></ui-gmap-marker>
</ui-gmap-google-map>
最佳答案
第一次点击后,您将重新定义一个没有点击事件的全新 map :
vm.map = {
center: {
latitude: coords[0].latLng.lat(),
longitude: coords[0].latLng.lng()
},
};
这将覆盖您之前设置的所有属性,包括click
。
改用setCenter
:
click: function(map, event, coords) {
var latLng = new google.maps.LatLng(latitude: coords[0].latLng.lat(), latitude: coords[0].latLng.lng());
vm.map.setCenter(latLng);
}
关于javascript - Angular Google Maps map 点击事件工作一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38061881/