开发 Maps JavaScript API Reference Manual Event Event

Event 最后更新时间: 2026年07月02日

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(String) Event type

function(Function) Callback function

context(Object) Event context, default is the instance itself

once(Boolean) Whether 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:(Object) Current 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(String) Event Type

function(Function) Event callback function

context(Object) Event context, default to the current instance

return value:(Object) Current Instance

Demo:

map.off("click",onClick)

hasEvents(type, function, context)

Determine if the current instance has bound an event callback

Parameter:  

type(String) Event type

function(Function) Event callback

context(Object) Event context

return value:(Boolean) Boolean 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(String) Event type, if this parameter is empty, clear all bound event callbacks on the current instance

return value:(Object) Current 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(String) Event type

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

return value:(Object) Current instance

Demo:

marker.emit('click') //Simulate triggering a certain event of the current instance
返回顶部 示例中心 常见问题 智能客服 公众号
二维码