简体   繁体   English

循环内的项目的单击事件触发两次

[英]Click event fires twice for item inside a loop

So the thing is that I have leafletjs map, using some data that I get from fetch I add new markers to the map.所以问题是我有leafletjs map,使用我从获取中获得的一些数据我将新标记添加到map。 There is a loop going through the data and it creates and puts a new marker for both "destination from" and "destination to".有一个循环遍历数据,它为“destination from”和“destination to”创建并放置一个新标记。

The problem is that after the click on one of the markers the event fires twice.问题是在单击其中一个标记后,事件会触发两次。 I have tried for example using a variable holding last click time and comparing it with a new one, but it does not fix the problem.例如,我尝试使用一个变量来保存最后一次点击时间并将其与新的比较,但它不能解决问题。 When I change the loop so it adds only 1 marker the event still fires twice.当我更改循环使其仅添加 1 个标记时,事件仍会触发两次。 Can you see what might be the cause?你能看出可能是什么原因吗?

The other additional problem (if someone can see why is that happening) is that after clicking one of the markers (from/to) (event fires twice) and click any other one (that doesn't belong to the previously clicked pair) I get undefined error on package.addressFrom另一个额外的问题(如果有人可以看到为什么会发生这种情况)是在单击一个标记(从/到) (事件触发两次)并单击任何其他标记(不属于先前单击的对)之后,我在package.addressFrom上得到未定义的错误

function prepare(data)
{
    for(var i=0; i < data.length; ++i)
    {
        var markeFrom = L.marker([data[i].latFrom, data[i].lngFrom], {myCustomClass: data[i].id}).addTo(myMapAll).on('click', markerClicked);
        var markerTo = L.marker([data[i].latTo, data[i].lngTo], {myCustomClass: data[i].id}).addTo(myMapAll).on('click', markerClicked);
        
        packageInfo["id"] = data[i].id;
        packageInfo["addressFrom"] = data[i].destinationFrom;
        packageInfo["addressTo"] = data[i].destinationTo;
        packageInfo["recipient"] = data[i].recipient;
        packageMapInfoArr.push(packageInfo);
    }
}

function markerClicked() 
{
    var clickedMarkerId = this.options.myCustomClass;
    var package = packageMapInfoArr.find(x => x.id === clickedMarkerId);
    
    alert('From:' + package.addressFrom + '\n' +
            'To:' + package.addressTo + '\n' +
            'Recipient:' + package.recipient);
}

在此处输入图像描述

Those screenshots seem to be from the Safari web browser, so chances are that you're hitting known Leaflet issue #7255 .这些屏幕截图似乎来自 Safari web 浏览器,因此您很可能遇到了已知的 Leaflet 问题 #7255 Using any other web browser should clarify if this is the case.使用任何其他 web 浏览器应该澄清是否是这种情况。

There is no clean solution to this bug at the time of this writing, but the current workaround is to disable the tap handler (by means of the tap option when initializing the L.Map instance)在撰写本文时,此错误尚无干净的解决方案,但当前的解决方法是禁用tap处理程序(通过初始化L.Map实例时tap选项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM