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

Angular中Leaflet Polyline右键菜单e.relatedTarget未定义的问题求助

Angular中Leaflet Polyline右键菜单e.relatedTarget未定义的问题求助

我在Angular项目里用了Leaflet(版本1.9.4)和Leaflet.contextmenu(版本1.4.0),做了一个带节点和连接线段的图形:

  • 节点是用Marker创建的,代码如下:
const options = {
  contextmenu: true,
  contextmenuInheritItems: false,
  contextmenuItems: this.buildMarkerContextMenu()
};
const marker = L.marker(coordinate, options);
(<any>marker).id = node.id;
  • 线段则是用Polyline创建:
const options = {
  contextmenu: true,
  contextmenuInheritItems: false,
  contextmenuItems: this.buildSectionContextMenu()
};
const line = L.polyline([coors1, coors2], options);
(<any>line).id = section.id;

把所有节点和线段都添加到地图后,我遍历地图的图层,能正常获取到所有带id的节点和线段,这部分没问题。

节点的右键菜单回调函数运行完全正常,能准确拿到点击的节点实例:

static showInformation(e: L.ContextMenuItemClickEvent) {
    console.log(e.relatedTarget);
    console.log((<any>e.relatedTarget).id);
}

可是线段的右键菜单就出问题了:菜单能正常显示,点击菜单项也能进入对应的回调函数,但e.relatedTarget居然是undefined:

static showSectionInformation(e: L.ContextMenuItemClickEvent) {
    console.log(e);
    console.log(e.relatedTarget); // !!! undefined
    //console.log((<any>e.relatedTarget).id);
}

我看Leaflet.contextmenu的示例都是针对地图和Marker的,是不是Polyline的右键菜单需要做什么特殊处理呀?

备注:内容来源于stack exchange,提问作者mli

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.14 15:14:27