2014-08-29 104 views
0

我已经在地图上设置了多边形围绕国家的地图,并且想要添加信息框以便在悬停时为每个国家显示一些信息。Bing地图多边形信息框

我可以在不使用多边形的情况下显示足够容易的信息框,但是当将它们分配给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); 

回答

0

没有在Bing地图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> 
+0

啊传说,谢谢。我自己一起砍了一些类似的东西,但是这样更干净。 – PizzaTheHut 2014-09-01 08:01:06