点击圈子 - 传单时启动一个功能 [英] start a function when click on circle - leaflet

查看:77
本文介绍了点击圈子 - 传单时启动一个功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

  function circleClick(){
//我的实现;
}

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

解决方案

只需在您的每个圈子中将 circleClick 作为听众分配:

  L.circle(
[46.765735535841024,23.58344078063965],5 ,{
color:blue
}
).addTo(map).on(click,circleClick);
//更多L.circle的...

函数circleClick(e){
var clickedCircle = e.target;

//做一些事情,比如:
clickedCircle.bindPopup(some content)。openPopup();
}

或者,您可以在功能组,并将事件监听器仅附加到该组:



< pre class =lang-js prettyprint-override> var group = L.featureGroup()。addTo(map);

L.circle(
[46.765735535841024,23.58344078063965],5,{
color:blue
}
).addTo(group);
//更多L.circle's ...

group.on(click,function(e){
var clickedCircle = e.layer; // e.target是团体本身。

//做一些事情,比如:
clickedCircle.bindPopup(some content)。openPopup();
});


I make some circles in JS as follow:

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

I want to replace that bindPopup with a function. When I click the circle, instead of my description display, I want to run a function, for example I made this function:

function circleClick() {
     // my implementations;
}

Would someone tell me how could I do this possible?

解决方案

Simply assign your circleClick function as listener on each of your circles:

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

Alternatively, you can gather all your circles within a Feature Group, and attach the event listener to that group only:

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

这篇关于点击圈子 - 传单时启动一个功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆