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

时间:2016-03-05 11:12:00

标签: javascript leaflet

我在JS中制作了一些圈子,如下所示:

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

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

function circleClick() {
     // my implementations;
}

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

2 个答案:

答案 0 :(得分:7)

只需在您的每个圈子中将numLines = 1 print("Hello world!") print("\033[<{0}>A".format(numLines), "This came AFTER hello world line") 功能指定为听众:

circleClick

或者,您可以在Feature Group内收集所有圈子,并将事件监听器仅附加到该组:

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

答案 1 :(得分:0)

您只需将圆圈指定给变量,然后听取点击事件。

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

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