简体   繁体   中英

How to add event handler to bing maps EntityCollection?

I use the BING Maps API to create a bar chart on the map. The char contains some polygons but I cannot add a event handler to the collection and all events I add to a polygon fires immediately.

var map = new Microsoft.Maps.Map(document.getElementById("mapDiv"), {credentials:"CREDENTIALS",
                            mapTypeId: Microsoft.Maps.MapTypeId.road,
                            zoom: 13,
                            center: new Microsoft.Maps.Location(51.363247,12.467959)});

var center = map.getCenter();
var chart = drawBarChart(map.getCenter(),10,20,30, 0.005);               
map.entities.push(chart);

...

function drawBarChart(location, value1, value2, value3, zoom) {

    var chart = new Microsoft.Maps.EntityCollection();

    var sum = value1 + value2 + value3;
    var height1 = value1 / sum * zoom;

    var maxHeight = Math.max(height1, height2, height3) + 0.1 * zoom;

    var rectPoints = new Array(5)
    rectPoints[0] = new Microsoft.Maps.Location(location.latitude - 0.1 * zoom, location.longitude - 0.1 * zoom);
    rectPoints[1] = new Microsoft.Maps.Location(location.latitude - 0.1 * zoom, location.longitude + 0.7 * zoom);
    rectPoints[2] = new Microsoft.Maps.Location(location.latitude + maxHeight, location.longitude + 0.7 * zoom);
    rectPoints[3] = new Microsoft.Maps.Location(location.latitude + maxHeight, location.longitude - 0.1 * zoom);
    rectPoints[4] = new Microsoft.Maps.Location(location.latitude - 0.1 * zoom, location.longitude - 0.1 * zoom);
            var black = new Microsoft.Maps.Color(200, 50, 50, 50);
    var white = new Microsoft.Maps.Color(200, 255, 255, 255);
    var transparent = new Microsoft.Maps.Color(0, 255, 255, 255);

    var border = new Microsoft.Maps.Polygon(rectPoints, {
        strokeColor: transparent,
        fillColor: white
    });
     chart.push(border);
     Microsoft.Maps.Events.addHandler(chart , 'mouseover', displayEventInfo);   
     return chart;

What's wrong?

Your problem here is that you try to add a 'mouseover' event on the EntityCollection but reffering to msdn the EntityCollection class does not support this kind of event.

I think you maybe want to add the event for each polygon, so edit your code like this :

Microsoft.Maps.Events.addHandler(border, 'mouseover', displayEventInfo); 

And all would work as you want.

Hope it help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM