Legacy
Mapbox.js is no longer in active development. To learn more about our newer mapping tools see Mapbox GL JS.
Events methods
A set of methods shared between event-powered classes (like Map). Generally, events allow you to execute some function when something happens with an object (e.g. the user clicks on the map, causing the map 'fire'
event).
Example
map.on('click', function(e) {
alert(e.latlng);
});
Leaflet deals with event listeners by reference, so if you want to add a listener and then remove it, define it as a function:
function onClick(e) { ... }
map.on('click', onClick);
map.off('click', onClick);
Methods
Method | Returns | Description |
---|---|---|
addEventListener(
|
this |
Adds a listener function (fn ) to a particular event type of the object. You can optionally specify the context of the listener (object the this keyword will point to). You can also pass several space-separated types (e.g. 'click dblclick' ). |
addOneTimeEventListener(
|
this |
The same as above except the listener will only get fired once and then removed. |
addEventListener(
|
this |
Adds a set of type/listener pairs, e.g. {click: onClick, mousemove: onMouseMove} |
removeEventListener(
|
this |
Removes a previously added listener function. If no function is specified, it will remove all the listeners of that particular event from the object. Note that if you passed a custom context to addEventListener , you must pass the same context to removeEventListener in order to remove the listener. |
removeEventListener(
|
this |
Removes a set of type/listener pairs. |
removeEventListener() |
this |
Removes all listeners. An alias to clearAllEventListeners when you use it without arguments. |
hasEventListeners(
|
Boolean |
Returns true if a particular event type has some listeners attached to it. |
fireEvent(
|
this |
Fires an event of the specified type. You can optionally provide an data object — the first argument of the listener function will contain its properties. |
clearAllEventListeners() |
this |
Removes all listeners to all events on the object. |
on( … ) |
this |
Alias to addEventListener . |
once( … ) |
this |
Alias to addOneTimeEventListener . |
off( … ) |
this |
Alias to removeEventListener . |
fire( … ) |
this |
Alias to fireEvent . |