Tuesday, May 14, 2024
 Popular · Latest · Hot · Upcoming
101
rated 0 times [  105] [ 4]  / answers: 1 / hits: 7732  / 8 Years ago, sat, march 5, 2016, 12:00:00

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?


More From » leaflet

 Answers
15

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

[#30348] Thursday, March 3, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mitchellg

Total Points: 235
Total Questions: 117
Total Answers: 106

Location: Fiji
Member since Wed, Jul 14, 2021
3 Years ago
;