尝试在地图框/传单中启用标记拖动时,无法读取 null 的属性“_leaflet_mousedown5”

Cannot read property '_leaflet_mousedown5' of null when attempting to enable marker dragging in Mapbox/Leaflet

本文关键字:null 读取 mousedown5 leaflet 属性 地图 单中 拖动 启用      更新时间:2023-09-26

我正在构建允许从CMS中编辑地图框标记的功能。单击地图标记时,该功能应打开并填充表单,然后允许拖动地图标记。保存表单后,将通过 ajax 提交内容,然后使用 featureLayer.loadURL("my_geojson_endpoint") 重新加载地图。

我在下面的代码中添加了注释,以概述我是如何到达错误的。

:注:我在 geojson 中定义了一个属性db_id来标识每个点,因为当您应用过滤器时,_leaflet_id会发生变化。我也在代码中包含jquery。

法典:

// loop through each marker, adding a click handler
$.each(points._layers, function (item) {
    var point = points._layers[item];
    attachClickHandler(point);
});
function attachClickHandler(point) {
    // open the edit state for the marker on click
    $(point._icon).on("click", function () {
        openEditState(point);
    })
}
function openEditState (point) {
    disableEditOthers(point);
    displayContent(point);
    point.dragging.enable(); // this line causes the error
    $(point._icon).off("click");
}
function disableEditOthers (point) {
    // hide the other markers from the map (using db_id as mentioned above)
    points.setFilter(function (f) {
        return f.db_id === point.feature.db_id;
    })
    // this functions as a callback to display the popup
    // since applying the filter on click, does not show the popup
    setTimeout(function () {
        for (key in points._layers) {
            points._layers[key].openPopup();
        }
    }, 0)
}

在地图创建步骤中,我已经能够在每个标记上调用这个 dragging.enable() 方法,并为所有这些标记提供"可拖动性",但从可用性的角度来看,这是不可取的。我希望用户清除交换进入和退出编辑状态。

我在 github 上发现了这个问题,通过解决方案解决了这个问题。但是,将我的mapbox.js版本换成独立版本并包括最新版本的传单(0.7.3)后,仍然发生了相同的错误。

我是否在对象的错误属性上调用该函数? 在错误的行之前转储"point"变量并不表明可拖动属性定义了enable()函数。

任何帮助都非常感谢。

好的,作为一个轻微的解决方法,但仍然没有解决原始错误。

$.each(points._layers, function (item) {
    points._layers[item].dragging.enable()
})

因为我已经过滤掉了其他点,所以启用所有点的拖动可以解决这个问题。

如果您可以提供对我原始修复程序的修复程序(避免循环),我很乐意接受它。