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

start a function when click on circle - leaflet

我在JS中画了一些圈子如下:

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

我想用函数替换 bindPopup。当我点击圆圈时,我想要 运行 一个功能,而不是我的描述显示,例如我做了这个功能:

function circleClick() {
     // my implementations;
}

有人能告诉我怎么做吗?

你只需要将圆赋值给一个变量,然后监听点击事件即可。

var circle = L.circle(...).addTo(map);

circle.on('click', function (e) {
    alert("Hello, circle!");
});

只需将您的 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();
}

或者,您可以将所有圈子集中在一个 Feature Group 中,并将事件侦听器仅附加到该组:

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();
});