必应地图多边形信息框

必胜客

我设置了一个地图,该地图在地图上的各个国家/地区周围都有多边形,并希望添加一个信息框,以便在悬停时会为每个国家/地区显示一些信息。

我可以使信息框在没有多边形的情况下显示起来也很容易,但是将它们分配给PolygonOptions类时,什么也没有发生。文档说,只要我加载了Bing Themes模块(我这样做),信息框就会在悬停时显示并单击。

似乎零文档/示例,因此希望您聪明的人可以帮忙。

这是一些相关的代码;

    var center = this.map.getCenter();

    // Create an info box 
    var infoboxOptions = {
        width: 300,
        height: 100,
        title: 'Testing', // sourceItems.data.dataset[0].data[index].key,
        description: "Visits: 20", // + sourceItems.data.dataset[0].data[index].visits,
        showPointer: true,
        titleClickHandler: this.polygonInfo,
        offset: new Microsoft.Maps.Point(-100, 0),
        typeName: Microsoft.Maps.InfoboxType.mini,
        zIndex: 1000
    };
    var polyinfobox = new Microsoft.Maps.Infobox(center, infoboxOptions);

    var polygonOptions = {
        fillColor: Microsoft.Maps.Color.fromHex(fillColour),
        strokeColor: Microsoft.Maps.Color.fromHex(fillColour),
        strokeThickness: 1,
        infobox: polyinfobox
    };

    var result = new Microsoft.Maps.Polygon(vertices, polygonOptions);
布伦德里特

交互式SDK中有一个适用于Bing Maps V7控件的工作代码示例,网址为http : //www.bingmapsportal.com/ISDK/AjaxV7#BingThemeModule6

请注意,为每个形状创建一个信息框的效率非常低,如果形状很多,则会损害性能。更好的方法是拥有一个信息框并动态填充其数据。我在这里写了一篇博客文章:http : //rbrundritt.wordpress.com/2011/10/13/multiple-pushpins-and-infoboxes-in-bing-maps-v7/如果您想在这里使用这种方法,代码示例:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
   <head>
      <title></title>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

      <script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0"></script>

      <script type="text/javascript">
        var map, dataLayer, infobox;

        function GetMap()
        {  
            map = new Microsoft.Maps.Map(document.getElementById("mapDiv"), {
                credentials: "YOUR_BING_MAPS_KEY"
            });

            dataLayer = new Microsoft.Maps.EntityCollection();
            map.entities.push(dataLayer);

            var infoboxLayer = new Microsoft.Maps.EntityCollection();
            map.entities.push(infoboxLayer);

            infobox = new Microsoft.Maps.Infobox(new Microsoft.Maps.Location(0, 0), { visible: false });
            infoboxLayer.push(infobox);

            AddData();
        }

        function AddData(){
            var polygon = new Microsoft.Maps.Polygon([
                new Microsoft.Maps.Location(45, -110), 
                new Microsoft.Maps.Location(65, -90), 
                new Microsoft.Maps.Location(45, -70)]);     

            polygon.Metadata = {
                title: 'Hello',
                description: 'World'
            };

            dataLayer.push(polygon);

            Microsoft.Maps.Events.addHandler(polygon, 'click', displayInfobox);
            Microsoft.Maps.Events.addHandler(polygon, 'mouseover', displayInfobox);
        }

        function displayInfobox(e){
            if(e.target){
                var point = new Microsoft.Maps.Point(e.getX(), e.getY());       
                var loc = map.tryPixelToLocation(point);

                infobox.setLocation(loc);

                var opt = e.target.Metadata;

                if(opt){
                    if(e.target.getIcon){ //is pushpin
                        opt.offset = new Microsoft.Maps.Point(0,20);
                    }else{
                        opt.offset = new Microsoft.Maps.Point(0,0);
                    }

                    opt.visible = true;
                    infobox.setOptions(opt);
                }
            }
        }
      </script>
   </head>
   <body onload="GetMap();">
      <div id='mapDiv' style="position:relative; width:600px; height:600px;"></div> 
   </body>
</html>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章