Document Maps JavaScript API Reference Manual Event Event

Event

All types of JS API (map, layers, overlays, etc.) implement event interfaces for binding, removing, and cleaning up event callbacks to the current instance object.

Prompt

The map JS API has a comprehensive event system, and in version 2.0, all types of instances use the on/off method to bind and remove events.

Demo

// Declare the callback function for the click event
function onClick(e) {
  console.log(e);
}
// Bind the onClick event to the map instance
map.on("click", onClick);

// Remove the onClick event binding from the map instance
map.off("click", onClick);

// Clear all click event bindings on the map instance
map.clearEvents("click");

// Overlay binds mouse move event
polygon.on("mousemove", console.log);

// Overlay event binding judgment
polygon.hasEvents("mousemove", console.log);

Method

on(type, function, context, once)

Bind event callback function to the instance, the same type, the same callback function, the same context will only be bound once

Parameter: 

type(StringEvent type

function(FunctionCallback function

context(ObjectEvent context, default is the instance itself

once(BooleanWhether to execute only once, default is false, the event callback function will not be removed, it will execute every time the event is triggered

return value:(ObjectCurrent Instance

Demo:

// Declare the callback function for the click event
function onClick(e) {
  console.log(e);
}
// Bind a click event to the map instance, onClick
map.on("click", onClick);

off(type, function, context)

Remove a specific event callback from the current instance

Parameter: 

type(StringEvent Type

function(FunctionEvent callback function

context(ObjectEvent context, default to the current instance

return value:(ObjectCurrent Instance

Demo:

map.off("click",onClick)

hasEvents(type, function, context)

Determine if the current instance has bound an event callback

Parameter:  

type(StringEvent type

function(FunctionEvent callback

context(ObjectEvent context

return value:(BooleanBoolean value indicating whether an event has been bound

Demo:

// Declare the callback function for the click event
function onClick(e) {
  console.log(e);
}
// Bind click event to overlay
polygon.on("click", onClick);
//Determine whether the polygon has bound the callback function onClick for the 'click' event
var bool = polygon.hasEvents("click", onClick);
console.log(bool); //Output: true

clearEvents(type)

Clear all event callbacks of a certain type for the current instance

Parameter: type(StringEvent type, if this parameter is empty, clear all bound event callbacks on the current instance

return value:(ObjectCurrent instance

Demo:

map.clearEvents("click") //Clear all event callbacks of type click for the map instance

emit(type, data)

Simulate the click event callback of the marker type

Parameter: 

type(StringEvent type

data(ObjectThe data returned during the event callback should be complete, otherwise it may cause errors

return value:(ObjectCurrent instance

Demo:

marker.emit('click') //Simulate triggering a certain event of the current instance