@angular/google-maps的(mapDrag)事件失效问题求助
解决@angular/google-maps标记拖拽事件不触发的问题
- 你绑定的
(mapDrag)是地图组件的专属事件,只会在拖拽地图本身时触发,和标记的拖拽行为无关,这就是为什么拖动标记时该事件没反应的核心原因。 - 标记的拖拽相关事件要使用
map-marker组件支持的输出事件,常用的有:(dragstart):标记开始拖拽时触发(drag):标记拖拽过程中持续触发(dragend):标记拖拽结束时触发
修正后的模板代码
<google-map [options]="options"> <map-marker #myMarker [position]="prueba" [draggable]="true" (drag)="hello()" ></map-marker> </google-map>
组件代码(无需修改核心逻辑,保持原样即可)
options: google.maps.MapOptions = { center: {lat: 10, lng: -70}, }; hello(){ console.log("Hello, you are dragging the marker") }
如果拖动标记时地图跟着联动,想阻止这种情况,可以给事件传递参数并阻止冒泡:
<!-- 模板修改 --> <map-marker #myMarker [position]="prueba" [draggable]="true" (drag)="hello($event)" ></map-marker>
// 组件方法修改 hello(event: any) { event.stopPropagation(); console.log("Hello, you are dragging the marker") }
内容的提问来源于stack exchange,提问作者Sebast.21
相关产品推荐
相关产品推荐

