0

我想在我的 bing 地图中添加多个信息框,这些信息框将在启动时自动加载,而不是在点击事件时加载。假设我有 3 个图钉,所以 3 个信息框会自动弹出数据,例如第一个信息框将包含 james、第二个标记、第三个等。截至目前,如果我把它放在一个循环中并尝试添加它总是采用最后一个值

 for (var i = 0; i < json.length; i++) {
                          var pinInfoBox;  //the pop up info box
                          var infoboxLayer = new Microsoft.Maps.EntityCollection();
                          var pinLayer = new Microsoft.Maps.EntityCollection();
                          var loc = jsonData.location[i];
                           var pushpinOptions = {icon: 'xxx.png', width: 80, height: 80, offset: new Microsoft.Maps.Point(0, 15)};
                         var infoboxOptions = {showCloseButton: true, visible:false };
                          pinInfobox = new Microsoft.Maps.Infobox(map.getCenter(), infoboxOptions );
                           pinInfobox.setHtmlContent('<div class="arrow_box"><div class="content" id="content">'+loc.name+'</div></div>'); //here the name should keep changing
                          infoboxLayer.push(pinInfobox);
                          var pin = new Microsoft.Maps.Pushpin(latLon,pushpinOptions);
                           pinLayer.push(pin); //add pushpin to pinLayer
                          Microsoft.Maps.Events.addHandler(pin, 'click', displayInfobox); //here instead of click event i want the infobox to display automatically on startup
                          map.entities.push(pinLayer);
                          map.entities.push(infoboxLayer);
           }

           function displayInfobox(e) {

        pinInfobox.setOptions({title: e.target.Title, description: e.target.Description,                  visible:true, offset: new Microsoft.Maps.Point(-50,90)});
      pinInfobox.setLocation(e.target.getLocation());
          }
4

1 回答 1

0

在 for 循环内而不是在循环之后设置信息框的选项和位置。然后在您的图钉中存储对每个信息框的引用。执行类似的pin.infobox = pinInfobox;操作,然后在 displayInfobox 函数中,您可以通过以下方式显示信息框:

e.target.infobox.setOptions({visible: true});

也就是说,如果您有很多图钉,最好有一个信息框并根据需要进行更新。这是概述此方法的博客文章:https ://rbrundrit.wordpress.com/2011/10/13/multiple-pushpins-and-infoboxes-in-bing-maps-v7/

于 2015-12-09T19:31:35.993 回答