none
Bing 8 click event - Access to originalEvent RRS feed

  • Question

  • I am currently migrating our product to Bing v8 from Bing v7.  We have a feature where you can hold down the ctrl key and click multiple locations to create a shape.  With Bing v8, I can not figure out how to get access to the originalEvent to see if the user is holding down the ctrl key.  Does anyone have a suggestion on how to do this in Bing v8?

    our current code is like this:

    function _MapMouseDownHandler(e) {
      if (!e.originalEvent.ctrlKey)
            _finish();
    }

    Thursday, April 6, 2017 5:38 PM

Answers

  • Bing Maps V8 doesn't provide access to the original event and likely won't in the future. To be honest I think you are only the first or second person to ask for this. That said, here is a code sample of how to accommodate your scenario:

    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
        <meta charset="utf-8" />
        <script type='text/javascript'>
        var map;
    
        function GetMap() {
            map = new Microsoft.Maps.Map('#myMap', {
                credentials: 'YourBingMapsKey'
            });
    
            var ctrlIsPressed = false;
    
            document.onkeydown = function (e) {
                ctrlIsPressed = e.ctrlKey;
            };
    
            document.onkeyup = function (e) {
                ctrlIsPressed = e.ctrlKey;
            };
                    
            Microsoft.Maps.Events.addHandler(map, 'mousedown', function (e) {
                if (ctrlIsPressed) {
                    alert('ctrl is pressed');
                }
            });
        }
        </script>
        <script type='text/javascript' src='https://www.bing.com/api/maps/mapcontrol?callback=GetMap' async defer></script>
    </head>
    <body>
        <div id="myMap" style=";width:600px;height:400px;"></div>
    </body>
    </html>

    Some background, the way events in Bing Maps V8 are significantly different from previous versions as all rendering occurs on an HTML5 canvas and thus all map related events need to be calculated. As an example, if you click on a pushpin in V7 the original event will have a reference to the pushpins DOM element, while in V8 he map knows a click occurs and calculates which pushpin was clicked. The DOM element in the original event for V8 would be the document or the map canvas.


    [Blog] [twitter] [LinkedIn]

    • Proposed as answer by Ricky_Brundritt Thursday, April 6, 2017 7:47 PM
    • Marked as answer by crsdew Friday, April 7, 2017 5:23 PM
    Thursday, April 6, 2017 7:47 PM

All replies

  • Bing Maps V8 doesn't provide access to the original event and likely won't in the future. To be honest I think you are only the first or second person to ask for this. That said, here is a code sample of how to accommodate your scenario:

    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
        <meta charset="utf-8" />
        <script type='text/javascript'>
        var map;
    
        function GetMap() {
            map = new Microsoft.Maps.Map('#myMap', {
                credentials: 'YourBingMapsKey'
            });
    
            var ctrlIsPressed = false;
    
            document.onkeydown = function (e) {
                ctrlIsPressed = e.ctrlKey;
            };
    
            document.onkeyup = function (e) {
                ctrlIsPressed = e.ctrlKey;
            };
                    
            Microsoft.Maps.Events.addHandler(map, 'mousedown', function (e) {
                if (ctrlIsPressed) {
                    alert('ctrl is pressed');
                }
            });
        }
        </script>
        <script type='text/javascript' src='https://www.bing.com/api/maps/mapcontrol?callback=GetMap' async defer></script>
    </head>
    <body>
        <div id="myMap" style=";width:600px;height:400px;"></div>
    </body>
    </html>

    Some background, the way events in Bing Maps V8 are significantly different from previous versions as all rendering occurs on an HTML5 canvas and thus all map related events need to be calculated. As an example, if you click on a pushpin in V7 the original event will have a reference to the pushpins DOM element, while in V8 he map knows a click occurs and calculates which pushpin was clicked. The DOM element in the original event for V8 would be the document or the map canvas.


    [Blog] [twitter] [LinkedIn]

    • Proposed as answer by Ricky_Brundritt Thursday, April 6, 2017 7:47 PM
    • Marked as answer by crsdew Friday, April 7, 2017 5:23 PM
    Thursday, April 6, 2017 7:47 PM
  • Ok. I will look into implementing a solution using he keyup/down functions.

    Thanks.

    Friday, April 7, 2017 5:24 PM