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

Ale*_*lex 4 javascript leaflet

我做了一些圈子JS如下:

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

我想bindPopup用一个函数替换它.当我点击圆圈而不是我的描述显示时,我想运行一个函数,例如我创建了这个函数:

function circleClick() {
     // my implementations;
}
Run Code Online (Sandbox Code Playgroud)

有人会告诉我怎么可能这样做?

ghy*_*ybs 7

只需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();
}
Run Code Online (Sandbox Code Playgroud)

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

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();
});
Run Code Online (Sandbox Code Playgroud)