单击圆圈时启动一个功能 - 传单

start a function when click on circle - leaflet

本文关键字:一个 功能 传单 启动 单击      更新时间:2023-09-26

我在JS中做了一些圆圈,如下所示:

L.circle(
  [46.765735535841024, 23.58344078063965], 5, {
    color: "blue"
  }).addTo(map).bindPopup("Description: This is my description");

我想用函数替换该bindPopup。当我单击圆圈时,我想运行一个函数,而不是我的描述显示,例如我制作了这个函数:

function circleClick() {
     // my implementations;
}

有人会告诉我我怎么可能做到这一点吗?

只需将circleClick函数指定为每个圈子的监听器:

L.circle(
  [46.765735535841024, 23.58344078063965], 5, {
    color: "blue"
  }
).addTo(map).on("click", circleClick);
// more L.circle's...
function circleClick(e) {
    var clickedCircle = e.target;
  // do something, like:
  clickedCircle.bindPopup("some content").openPopup();
}

或者,您可以将所有圈子收集到功能组中,并仅将事件侦听器附加到该组:

var group = L.featureGroup().addTo(map);
L.circle(
  [46.765735535841024, 23.58344078063965], 5, {
    color: "blue"
  }
).addTo(group);
// more L.circle's...
group.on("click", function (e) {
    var clickedCircle = e.layer; // e.target is the group itself.
  // do something, like:
  clickedCircle.bindPopup("some content").openPopup();
});

你只需要把圆分配给一个变量,然后监听点击事件。

var circle = L.circle(...).addTo(map);
circle.on('click', function (e) {
    alert("Hello, circle!");
});