【问题标题】:Bing Maps Polygon InfoBox必应地图多边形信息框
【发布时间】:2014-08-29 17:04:07
【问题描述】:

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

如果没有多边形,我可以很容易地显示信息框,但是将它们分配给 PolygonOptions 类时,什么也没有发生。文档说,只要我加载了 Bing 主题模块(我这样做了),信息框就会在悬停和单击时显示。

这方面的文档/示例似乎为零,因此希望各位聪明人能提供帮助。

这里是一些相关代码;

    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);

【问题讨论】:

    标签: maps bing-maps bing infobox


    【解决方案1】:

    Bing Maps V7 控件的交互式 SDK 中有一个工作代码示例: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>
    

    【讨论】:

    • 啊传奇,谢谢。我自己拼凑了类似的东西,但这更干净。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多