简体   繁体   English

传单标记事件在错误的时间触发

[英]Leaflet marker event fires at wrong time

I am working with leaflet (v 0.7.7) and I have an Ajax call that gets some server data to bind to my map in the form of clickable text labels.我正在使用传单 (v 0.7.7) 并且我有一个 Ajax 调用,它获取一些服务器数据以可点击文本标签的形式绑定到我的地图。 In the loop where I am binding the JSON data that I got from the server I have the following code:在我绑定从服务器获得的 JSON 数据的循环中,我有以下代码:

var item_name = L.marker([data.X, data.Y],
{
    icon: L.divIcon(
    {
        className:'MapTag', 
        iconAnchor: [10, 10],
        html:'<img src="/Images/Map/Item' + data.Id + '.png">' + data.Name 
    })
}).on('click', onItemClick(data.Id));

item_layer.addLayer(item_name);

Elsewhere, I have the onItemClick code:在其他地方,我有 onItemClick 代码:

function onItemClick(item_id)
{
    alert(item_id);
}

Now, the good news, if I comment out the event binding part of that, the loop completes, and leaflet behaves as it should.现在,好消息是,如果我注释掉其中的事件绑定部分,循环就会完成,并且传单的行为会正常。 However, when I bind the click event, things get odd.但是,当我绑定 click 事件时,事情变得很奇怪。 The event is fired once for each item in the collection I am binding to.该事件为我绑定的集合中的每个项目触发一次。 When the data is loaded from the server, I get an alert popping up for each time through the loop with the current item's Id.当数据从服务器加载时,我每次通过带有当前项目 ID 的循环都会弹出一个警报。 It feels like it is being fired 'onload' rather than 'onclick'.感觉它是被“onload”而不是“onclick”触发的。 To top it off, it also doesn't register clicks on the divIcons after loading.最重要的是,它也不会在加载后注册 divIcons 上的点击。

There must be something I am missing here, but I cannot see what it is.一定有什么我在这里失踪了,但我看不到它是什么。 Any suggestions?有什么建议?

THis question is similar to ( Marker in leaflet, click event )这个问题类似于( 传单中的标记,点击事件

Resolution: I changed the last line of the divIcon declaration to:解决方案:我将 divIcon 声明的最后一行更改为:

}).on('click', function(e){ alert(data.Id); });

This now works as intended.这现在按预期工作。 I am guessing that the strange binding behavior is a result of not binding a defined method reference and leaflet having some sort of functional meltdown in its event management code.我猜测奇怪的绑定行为是由于未绑定定义的方法引用和传单在其事件管理代码中具有某种功能崩溃。

I kept the 'e' argument in, since it is what the leaflet docs show.我保留了“e”参数,因为它是传单文档显示的内容。 I am not using it, but if someone else copy-pastes this, they might need it.我没有使用它,但如果其他人复制粘贴它,他们可能需要它。

You're confusing the concepts of function references and function calls, and you are not making a closure over the item ID.您混淆了函数引用和函数调用的概念,并且您没有对项目 ID 进行闭包

If you declare this:如果你声明:

function onItemClick(id) { alert(id); }

This prints the reference to the function:这将打印对函数的引用:

console.log( onItemClick );

And this prints the return value of calling that function (as it returns nothing, this equals undefined ):这将打印调用该函数的返回值(因为它不返回任何内容,这等于undefined ):

console.log( onItemClick(5) );

So when you're doing this:所以当你这样做时:

L.marker(....).on('click', onItemClick(id) );

the function gets called, and on() receives the return value of that function, ie:该函数被调用,并且on()接收该函数的返回值,即:

L.marker(....).on('click', undefined );

What you want to do is have a function that returns a function :您想要做的是有一个返回函数的函数

function onItemClick(id) { return function(){ alert(id); } }

This way, when you do这样,当你做

L.marker(....).on('click', onItemClick(5) );

That will make a function call, and use the return value, which now looks like this:这将进行一个函数调用,并使用返回值,现在看起来像这样:

L.marker(....).on('click', function() { alert(5); } );

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

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