简体   繁体   English

如何定位此数组中的索引?

[英]How can I target an index in this array?

I am trying to add rename and delete functionality to my map, but I have thus far been unable to target a single value in the markers array. 我正在尝试向地图添加重命名和删除功能,但是到目前为止,我一直无法定位markers数组中的单个值。 I tried using markers.indexOf(marker) as seen below: 我尝试使用markers.indexOf(marker),如下所示:

L.DomEvent.on(deleteBtn, 'click', function(ev) {
        var index =  markers.indexOf(marker);
          console.log(marker.indexOf(index));
           console.log('Delete button clicked!');
        }, this);
        });

When I do this, I get all markers on the map in console. 完成此操作后,我将在控制台中的地图上看到所有标记。 When I use the array.find method, I get back false indicating the value doesn't exist, but each marker is an object stored in local storage.The objective is to be able to retrieve an index of a member of the markers array to grab values from local storage to change them as needed. 当我使用array.find方法时,我返回false指示该值不存在,但每个标记都是存储在本地存储中的一个对象,目的是能够检索markers数组成员的索引以从本地存储中获取值以根据需要更改它们。

 // Creates the custom markers added by the user and stores them to localStorage
    var markers = [];
    var customLayer = new L.layerGroup();
    myMap.on('contextmenu', function(e) {
          var marker = L.marker(e.latlng,
          {icon : flagIcon}).addTo(customLayer).addTo(myMap);
          marker.bindTooltip("<b>Custom</b>", {permanent: true, offset: [0, 0],direction: "bottom"});
          customLayer.addTo(myMap);
          markers.push({ coords: e.latlng, name: "<b>Custom</b>" });

          marker.on('click', function(e) {

            renameDeleteChoice(e);
            L.DomEvent.on(renameBtn, 'click', function(ev) {

                console.log('Rename button clicked!');
            }, this);

            L.DomEvent.on(deleteBtn, 'click', function(ev) {
           // I have been trying code here
               console.log('Delete button clicked!');
            }, this);
            });

    // Save marker Coords to localStorage

          markers.forEach(function() {
            window.localStorage.setItem('customData', JSON.stringify(markers));

       });
    });

Did I just set this up wrong? 我只是把这个设置错了吗? Is my array that I push new values to supposed to have this behavior? 我推送新值的数组是否应该具有这种行为? Here's the documentation for leaflet in case anyone needs it. 如果有人需要,这里是传单的文档。 I'd be happy to answer any questions for clarification. 我很乐意回答任何问题以求澄清。

https://leafletjs.com/reference-1.3.4.html https://leafletjs.com/reference-1.3.4.html

Thanks in advance for any guidance you provide. 在此先感谢您提供的任何指导。

You could add a unique id to the marker variable that is also referenced in the custom object, and then find the item based off that. 您可以将一个唯一的ID添加到标记变量中,该标记也在自定义对象中也被引用,然后根据该变量找到项目。 If the latlng value is unique you could do something like: 如果latlng值是唯一的,则可以执行以下操作:

myMap.on('contextmenu', function(e) {
    var marker = ...;
    marker.id = e.latlng; 
    markers.push({ coords: e.latlng, name: "<b>Custom</b>" });

    marker.on('click', function(e) {
        L.DomEvent.on(deleteBtn, 'click', function(ev) {
            var markerId = marker.id;
            var markerItem = markers.find(function(m) {
                return m.coords === markerId;
            });
            // update markerItem in some way
            // alternatively, use findIndex if you really just want the index
        }, this);
    });
 });

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

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