You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

@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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 05:07:22