1. <tfoot id='XqtL6'></tfoot>
    2. <legend id='XqtL6'><style id='XqtL6'><dir id='XqtL6'><q id='XqtL6'></q></dir></style></legend>
    3. <small id='XqtL6'></small><noframes id='XqtL6'>

        <bdo id='XqtL6'></bdo><ul id='XqtL6'></ul>
      <i id='XqtL6'><tr id='XqtL6'><dt id='XqtL6'><q id='XqtL6'><span id='XqtL6'><b id='XqtL6'><form id='XqtL6'><ins id='XqtL6'></ins><ul id='XqtL6'></ul><sub id='XqtL6'></sub></form><legend id='XqtL6'></legend><bdo id='XqtL6'><pre id='XqtL6'><center id='XqtL6'></center></pre></bdo></b><th id='XqtL6'></th></span></q></dt></tr></i><div id='XqtL6'><tfoot id='XqtL6'></tfoot><dl id='XqtL6'><fieldset id='XqtL6'></fieldset></dl></div>

      Leaflet.contextmenu 回调

      时间:2023-08-08
        <bdo id='2hXWR'></bdo><ul id='2hXWR'></ul>

        <legend id='2hXWR'><style id='2hXWR'><dir id='2hXWR'><q id='2hXWR'></q></dir></style></legend>

            <small id='2hXWR'></small><noframes id='2hXWR'>

          1. <i id='2hXWR'><tr id='2hXWR'><dt id='2hXWR'><q id='2hXWR'><span id='2hXWR'><b id='2hXWR'><form id='2hXWR'><ins id='2hXWR'></ins><ul id='2hXWR'></ul><sub id='2hXWR'></sub></form><legend id='2hXWR'></legend><bdo id='2hXWR'><pre id='2hXWR'><center id='2hXWR'></center></pre></bdo></b><th id='2hXWR'></th></span></q></dt></tr></i><div id='2hXWR'><tfoot id='2hXWR'></tfoot><dl id='2hXWR'><fieldset id='2hXWR'></fieldset></dl></div>
          2. <tfoot id='2hXWR'></tfoot>
              <tbody id='2hXWR'></tbody>

              • 本文介绍了Leaflet.contextmenu 回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在努力解决 Leaflet.contextmenu 库中的问题.

                I'm struggling with a problem in the Leaflet.contextmenu library.

                我有许多不同的地图,保存在一个全局数组中.然后我使用 contextmenu 选项在我的地图中有一个 contextmenu.当我想定义我的回调函数时,我无法访问我的 arrMap[id],因为该函数不知道我正在使用的 id.

                I got a number of different maps, saved in a global Array. Then I'm using the contextmenu options to have a contextmenu in my maps. When I want to define my callback functions, I can't access my arrMap[id], because the function doesn't know the id I'm using.

                我的问题是:如何将对象(例如 id)放入 Leaflet.contextmenu 库的回调函数中?

                My question here is: How can I give an object (the id, for example) into a callback function of the Leaflet.contextmenu library?

                function x(){
                    arrMap[id] = new L.map('map'+id,{
                        contextmenu: true,
                        contextmenuWidth: 140,
                        contextmenuItems: [{
                            text: 'Messung an dieser Position einfügen',
                            callback: newMeasurement
                        }, {
                            text: 'zeige Koordinaten',
                            callback: showCoordinates
                        }, {
                            text: 'Karte hier zentrieren',
                            callback: centerMap
                        }]
                    });
                }
                
                function newMeasurement(e){
                //do something with e AND ID
                }
                

                我想过这样的事情:

                //function x(){...
                callback: newMeasurement(e,id)
                //...}
                
                function newMeasurement(e,id){
                  console.log(id);
                }
                

                ...但这不起作用:(

                ...but that's not working :(

                感谢大家的帮助!

                推荐答案

                你需要为你想要的值创建一个闭包.

                You need to create a closure over the value you want.

                首先,阅读 «JS 闭包如何工作?» 问题.

                然后,阅读 MDN 闭包参考.然后,this question about how to create different Leaflet event handlers pass每个处理函数的值不同

                首先阅读这些内容.尝试理解这个概念.我是认真的.如果你一味地复制粘贴代码,那么stackoverflow的神会杀了一只小猫.

                Read those first. Try to understand the concept. I mean it. If you blindly copy-paste code, then the gods of stackoverflow will kill a kitten.

                现在,你想要一个事件处理函数,它只接收一个参数,比如

                Now, you want to have an event handler function, which will receive just one parameter, like

                function newMeasurement(ev){
                    // do something with 'ev' AND 'id'
                }
                

                那个函数需要接收一个参数,并且需要在某处有一个id变量.好的,那么,让我们创建一个返回函数的函数:

                That function needs to receive one parameter, and needs to have an id variable somewhere. OK then, let's create a function that returns a function:

                function getMeasurementHandler(id) {
                    return function(ev) {
                        doSomething(ev, id);
                    }
                }
                

                这样,如果你运行例如:

                That way, if you run e.g.:

                var handlerForId1234 = getMeasurementHandler(1234);
                

                这或多或少等同于

                var handlerForId1234 = function(ev) { doSomething(ev, 1234); }
                

                让我们把它们放在一起:

                Let's put it all together:

                for (var id=0; id<4; id++) {
                    arrMap[id] = new L.map('map'+id, {
                        contextmenuItems: [{
                            text: 'Somethingsomething',
                            callback: getEventHandlerForId(id)
                        }]
                    });
                }
                
                getCallbackFuncForId(id) {
                    return function(ev) {
                        console.log('Event ', ev, ' in ID ', id);
                    }
                }
                

                这篇关于Leaflet.contextmenu 回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何更改 rmarkdown 单元格中的 css &amp;闪亮的 下一篇:如何将数据从 Flask 发送到 JavaScript?

                相关文章

                最新文章

              • <small id='6oTcU'></small><noframes id='6oTcU'>

                  <i id='6oTcU'><tr id='6oTcU'><dt id='6oTcU'><q id='6oTcU'><span id='6oTcU'><b id='6oTcU'><form id='6oTcU'><ins id='6oTcU'></ins><ul id='6oTcU'></ul><sub id='6oTcU'></sub></form><legend id='6oTcU'></legend><bdo id='6oTcU'><pre id='6oTcU'><center id='6oTcU'></center></pre></bdo></b><th id='6oTcU'></th></span></q></dt></tr></i><div id='6oTcU'><tfoot id='6oTcU'></tfoot><dl id='6oTcU'><fieldset id='6oTcU'></fieldset></dl></div>

                  1. <legend id='6oTcU'><style id='6oTcU'><dir id='6oTcU'><q id='6oTcU'></q></dir></style></legend>

                      <bdo id='6oTcU'></bdo><ul id='6oTcU'></ul>
                  2. <tfoot id='6oTcU'></tfoot>