如何将事件处理程序添加到bing映射EntityCollection?

时间:2013-04-02 13:06:28

标签: javascript bing-maps

我使用BING Maps API在地图上创建条形图。 char包含一些多边形但我无法向集合中添加事件处理程序,并且我添加到多边形的所有事件都会立即触发。

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;

怎么了?

1 个答案:

答案 0 :(得分:1)

你的问题是你尝试在EntityCollection上添加'mouseover'事件但是向msdn提供EntityCollection类不支持这种事件。

我想你可能想为每个多边形添加事件,所以编辑你的代码如下:

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

所有这些都可以按你的意愿运作。

希望有所帮助。