0

I made some markers on a map using

L.marker([42.76, 20.52], 5, {color: "blue"}).addTo(map).bindPopup("<b>Name: </b> My name<br /><b>Adress:</b> My adress<br /> <b>Description:</b>My description...").on("click", circleClick);

My circleClick function is the following:

function circleClick(e) {
app.getInfo(e.latlng.lat, e.latlng.lng);;}

I use this function to send the coordinates from JavaScript to Java. But now I want to add some parameters to my circleClick function. That's not a problem, I know how to do this. But I don't know how to call the function with parameters. I tried:

L.marker([42.76, 20.52],[...]).on("click", circleClick(e, parameter1, parameter2));

but it doesn't work. Please help me, how to specify the parameters at .on("click", circleClick(...));?

Alex
  • 139
  • 3
  • 10

2 Answers2

4

If you don't mind about the order of the parameters you can bind it to prepend parameters:

.on("click", circleClick.bind(null, parameter1, parameter2));

it will create function with prepended parameter1, parameter2 so that the callback will be called like circleClick(parameter1, parameter2, e)

madox2
  • 49,493
  • 17
  • 99
  • 99
2
L.marker([42.76, 20.52],[...]).on("click", function(e) {
  circleClick(e, parameter1, parameter2);
});

From the jquery doc: The handler argument is a function (or the value false, see below), and is required unless you pass an object for the events argument. What you're doing is calling the function in the on() method and not specifying the handler.

Rahul Sharma
  • 5,562
  • 4
  • 24
  • 48
  • I added that. But my function still doens't work: `function circleClick(e, name, adress) { app.getInfo(e.latlng.lat, e.latlng.lng, name, adress); }` – Alex Mar 26 '16 at 10:44
  • See this [fiddle](https://jsfiddle.net/n8sqf63q/). If you're still having issues, then there might be something else wrong with the code. Try putting logs. – Rahul Sharma Mar 26 '16 at 11:04
  • For the record, please note that Leaflet is not based on jQuery, even though it follows a similar API for attaching event listeners, like many other libraries. – ghybs Mar 27 '16 at 05:34