none
How to pass parameters within infoboxoptions action function RRS feed

  • Question

  • I want to pass some parameters within Handler function,
    Here is my code
    Can any one help , How to do this 

    infoboxOptions = new Microsoft.Maps.Infobox(Point,
                   {
                       title: 'Title',
                       visible: true,
                       offset: new Microsoft.Maps.Point(0, 15),
                       actions: [{ label: 'Handler1', eventHandler: HandlerFunction }]
                   });

    function HandlerFunction (){

    }

    what I want is something Like below code...


    infoboxOptions = new Microsoft.Maps.Infobox(Point,
                  {

     actions: [{ label: 'Handler1', eventHandler: HandlerFunction(Title, Desc, Lat) }]

    }

    function HandlerFunction(Title, Desc, Lat) {}

    Thursday, July 7, 2016 8:53 AM

Answers

  • This is fairly easy to do in JavaScript. You simply need to create a function that takes in your properties and returns a callback function. You will then be able to access those properties from your callback function. Here is an example:

    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
        <meta charset="utf-8" />
        <script type='text/javascript' 
                src='http://www.bing.com/api/maps/mapcontrol?callback=GetMap' 
                async defer></script>
        <script type='text/javascript'>
        var map, infobox;
    
        function GetMap() {
            map = new Microsoft.Maps.Map('#myMap', {
                credentials: 'Your Bing Maps Key'
            });
    
            var center = map.getCenter();
    
            infobox = new Microsoft.Maps.Infobox(center, {
                actions: [{
                    label: 'Handler',
                    eventHandler: createHandler('hello','',0)
                }]
            });
    
            //Assign the infobox to a map instance.
            infobox.setMap(map);
        }
    
        function createHandler(title, desc, lat) {
            return function () {
                alert('Title: ' + title);
            }
        }
        </script>
    </head>
    <body>
        <div id="myMap" style=";width:600px;height:400px;"></div>
    </body>
    </html>


    [Blog] [twitter] [LinkedIn]


    Thursday, July 7, 2016 6:47 PM