DrawingManager Events

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.

The example adds a bunch of events to the drawing manager that is created by the drawing tools module. As the different events first a label will be highlighted in green to indicate which event(s) have fired.

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
	<script type='text/javascript'>
    var map, drawingManager;

    function GetMap() {
        map = new Microsoft.Maps.Map('#myMap', {});

        //Load the DrawingTools module
        Microsoft.Maps.loadModule('Microsoft.Maps.DrawingTools', function () {
            //Create an instance of the DrawingTools class and bind it to the map.
            var tools = new Microsoft.Maps.DrawingTools(map);

            //Show the drawing toolbar and enable editting on the map.
            tools.showDrawingManager(function (manager) {

                //Add events to the drawing manager.
                Microsoft.Maps.Events.addHandler(manager, 'drawingChanged', function () { highlight('drawingChanged'); });
                Microsoft.Maps.Events.addHandler(manager, 'drawingChanging', function () { highlight('drawingChanging'); });
                Microsoft.Maps.Events.addHandler(manager, 'drawingEnded', function () { highlight('drawingEnded'); });
                Microsoft.Maps.Events.addHandler(manager, 'drawingErased', function () { highlight('drawingErased'); });
                Microsoft.Maps.Events.addHandler(manager, 'drawingModeChanged', function () { highlight('drawingModeChanged'); });
                Microsoft.Maps.Events.addHandler(manager, 'drawingStarted', function () { highlight('drawingStarted'); });
            })
        });
    }

    function highlight(id) {
        //Highlight the mouse event div to indicate that the event has fired.
        document.getElementById(id).style.background = 'LightGreen';

        //Remove the highlighting after a second.
        setTimeout(function () { document.getElementById(id).style.background = 'white'; }, 1000);
    }
    </script>
    <script type='text/javascript' src='http://www.bing.com/api/maps/mapcontrol?callback=GetMap&key=[YOUR_BING_MAPS_KEY]' async defer></script>
</head>
<body>
    <div id="myMap" style="position:relative;width:600px;height:400px;"></div>

    <div id="drawingChanged">drawingChanged</div>
    <div id="drawingChanging">drawingChanging</div>
    <div id="drawingEnded">drawingEnded</div>
    <div id="drawingErased">drawingErased</div>
    <div id="drawingModeChanged">drawingModeChanged</div>
    <div id="drawingStarted">drawingStarted</div>
</body>
</html>

Running this code in a browser you will see the different events highlighted in green as you draw a shape on the map.

Screenshot of a Bing map showing a polygon shape overlaid on top of Redmond, Washington, and a list of events below the map.