如何通过带有传单的按钮单击来更改标记的图标?

How to change the icon of a marker through button click with leaflet?

我有一张带有一些标记的地图。每个标记都有一个带有 3 个按钮的信息窗口,如下所示:

点击每个按钮都会更改标记的图标。但是,当我打开一个标记的信息窗口并且不单击任何按钮,然后转到另一个标记并单击其中一个按钮时,两个标记都会更改图标,而不是仅更改最后一个点击的图标。 这是我的代码:

//Get de todas as ignições presentes na base de dados
$.get("/api/IgnicoesAPI", function (data) {
  //alert(aceite)
  console.log(data);

  $.each(data, function (i, item) {
    //identificação do tipo de marcador que deve aparecer de acordo com o estado da ignição
    var ignicao;

    // MORE CODE

    var id = item.id;

    //colocar um marcador no mapa de acordo com a latitude e longitude fornecidas
    var marker = new L.marker([item.latitude, item.longitude], {
      icon: ignicao,
    })
      .on("click", function onClick(e) {
        //assim que um marcador for clicado é mostrado o popup das ignições
        modal.style.display = "block";

        //indicação do marcador que foi clicado
        clickedmarker = e.target;
        console.log(clickedmarker);

        //vai buscar toda a informação correspondente ao id fornecido
        getData(id);

        //Actividade dos botões presentes no popup das ignições
        $(document).on("click", "#aceite", function () {
          //se o estado for aceite, o botão respetivo estará desativado
          if (item.estado == aceite) {
            document.getElementById("aceite").disabled = true;
            document.getElementById("recusado").disabled = false;
            document.getElementById("concluido").disabled = false;
          }
          //se for clicado passará ao icon correspondente
          else {
            clickedmarker.setIcon(accepted);
            //fecha o modal das avaliação da ignição
            modal.style.display = "none";
            //atualiza a base de dados com o novo estado
            atualizaBD(id, Estado.aceite, item.latitude, item.longitude);
          }
        });

        $(document).on("click", "#concluido", function () {
          //se o estado for concluido, o botão respetivo estará desativado
          if (item.estado == concluido) {
            document.getElementById("concluido").disabled = true;
            document.getElementById("aceite").disabled = false;
            document.getElementById("recusado").disabled = false;
          }
          //se for clicado passará ao icon correspondente
          else {
            clickedmarker.setIcon(conclued);
            //fecha o modal das avaliação da ignição
            modal.style.display = "none";

            //atualiza a base de dados com o novo estado
            atualizaBD(id, Estado.concluido, item.latitude, item.longitude);
          }
        });

        $(document).on("click", "#recusado", function () {
          //se o estado for recusado, o marcador será removido do mapa
          //clickedmarker.removeFrom(map);

          //map.removeLayer(clickedmarker)

          map.removeLayer(marker);

          modal.style.display = "none";
          //atualiza a base de dados com o novo estado
          atualizaBD(id, Estado.recusado, item.latitude, item.longitude);
        });
      })
      .addTo(map);

    //adiciona marador ao mapa
    $("#json map").append(marker);

    if (item.estado == recusado) {
      map.removeLayer(marker);
    }
  }); // fim each
}); //fim do get

我该如何解决这个问题?

这是一个关于 closures 和过于急切地附加事件处理程序的问题,这最终会使您的事件处理程序 运行 次数超出您的预期。

您在每次标记点击时附加 jQuery 事件处理程序 ,所以如果您有类似的代码...

var marker = new L.marker(/* stuff */).on("click", function onClick(ev) {
    $(document).on("click", "#aceite", function () {
        console.log('running click handler');
    });
});

...然后你点击一个标记,比如说,10 次,这与附加 jQuery 单击标记 10 次具有相同的效果:

    $(document).on("click", "#aceite", function () {
        console.log('running click handler');
    });
    $(document).on("click", "#aceite", function () {
        console.log('running click handler');
    });
    $(document).on("click", "#aceite", function () {
        console.log('running click handler');
    });
    $(document).on("click", "#aceite", function () {
        console.log('running click handler');
    });
    $(document).on("click", "#aceite", function () {
        console.log('running click handler');
    });
    $(document).on("click", "#aceite", function () {
        console.log('running click handler');
    });
    $(document).on("click", "#aceite", function () {
        console.log('running click handler');
    });
    $(document).on("click", "#aceite", function () {
        console.log('running click handler');
    });
    $(document).on("click", "#aceite", function () {
        console.log('running click handler');
    });
    $(document).on("click", "#aceite", function () {
        console.log('running click handler');
    });

因此,如果您随后单击该按钮一次,代码将 运行 10 次。

您感到困惑,因为 id 存在于循环范围内,而 jQuery 单击处理函数是在所述循环内定义的。因此,如果我们暂时假设您有 ID 为 4 和 5 的项目,并且您在代码中为每个项目的每个标记单击 一次...

$.each(数据, 函数 (i, item) { 变量 ID = item.id;

var marker = new L.marker(/* stuff */).on("click", function onClick(ev) {
    $(document).on("click", "#aceite", function () {
        console.log('running click handler with ID', id);
    });
});

...这相当于附加两个不同的单击事件处理程序,每个处理程序对 closure (since they live in different scopes):

具有不同的值
    $(document).on("click", "#aceite", function () {
        console.log('running click handler with ID', 4);
    });        
    $(document).on("click", "#aceite", function () {
        console.log('running click handler with ID', 5);
    });

因此,如果您随后单击该按钮一次,代码将 运行 两次。

除非你真的确定你在做什么(即你正在跟踪有多少事件处理程序附加到一个事件并根据需要分离它们),避免在循环内和内部附加事件处理程序其他事件处理程序。

所以而不是...

data.forEach(function (item, i) {
    var id = item.id;
    L.marker(item.latlng).on('click', function(ev) {
        $("#button").on('click', function() {
            console.log('Doing stuff for item', id);
        });
    });
});

...你应该尽量让事情保持 运行 一次(即附加 jQuery 事件处理程序)运行ning 一次,并将任何需要的状态提升到通用范围,例如...

// 'id' exists out of the scope of any functions defined inside the loop,
// so it ""exists only once"" to the eyes of those functions
var id;    

data.forEach(function (item, i) {
    L.marker(item.latlng).on('click', function(ev) {
        // Since the marker click handler function is defined within a loop,
        // and the scope of 'item' is that of the loop, it forms a closure, 
        // which means it's ""unique"" to each of the marker click handler
        // functions.
        // By contrast, 'id' is defined outside of that scope, so it's
        // ""common"" to all of the marker click handler functions
        id = item.id;
    });
}); 

// Attach the jQuery event handler **once**, and do not wait
// until clicking on a marker to do so.
$("#button").on('click', function() {
    console.log('Doing stuff for item', id);
});

请继续阅读 closures。真的。