Events Class

Note

Bing Maps Web Control SDK retirement

Bing Maps Web Control SDK is deprecated and will be retired. Free (Basic) account customers can continue to use Bing Maps Web Control SDK until June 30th, 2025. Enterprise account customers can continue to use Bing Maps Web Control SDK until June 30th, 2028. To avoid service disruptions, all implementations using Bing Maps Web Control SDK will need to be updated to use Azure Maps Web SDK by the retirement date that applies to your Bing Maps for Enterprise account type. For detailed migration guidance, see Migrate from Bing Maps Web Control SDK and Migrate Bing Maps Enterprise applications to Azure Maps with GitHub Copilot.

Azure Maps is Microsoft's next-generation maps and geospatial services for developers. Azure Maps has many of the same features as Bing Maps for Enterprise, and more. To get started with Azure Maps, create a free Azure subscription and an Azure Maps account. For more information about azure Maps, see Azure Maps Documentation. For migration guidance, see Bing Maps Migration Overview.

Events can be added and removed by using the methods available through the Microsoft.Maps.Events class. The Events class has the following static methods available.

Name Type Description
addHandler(target:object, eventName:string, handler:function) object Attaches the handler for the event that is thrown by the target. Use the return object to remove the handler using the removeHandler method.
addOne(target:object, eventName:string, handler:function) Attaches the handler for the event that is thrown by the target, but only triggers the handler the first once after being attached.
addThrottledHandler(target:object, eventName:string, handler:function, throttleInterval:number) object Attaches the handler for the event that is thrown by the target, where the minimum interval between events (in milliseconds) is specified as a parameter.
hasHandler(target:object, eventName:string) boolean Checks if the target has any attached event handler.
invoke(target:object, evenName:string,args:object) Invokes an event on the target. This causes all handlers for the specified event name to be called.
removeHandler(handlerId: object) Detaches the specified handler from the event. The handlerId is returned by the addHandler and addThrottledHandler methods.

The following code sample demonstrates how to add and remove events. The target is the object you want to add the event to (i.e. the map), the eventName is the name of the event to attach as a string, and the handler is a callback function that is called when the event is triggered. When an event is added, the addHandler method returns an object (handlerId). This object can be passed to the removeHandler method later to detach the event from the target object.

//Attach an event
var handlerId = Microsoft.Maps.Events.addHandler(target, 'eventName', handler);

//Remove an event
Microsoft.Maps.Events.removeHandler(handlerId);

See Also