我想在我的bing地图中添加多个信息框,这些地图将在启动时自动加载而不是点击事件。假设我有3个图钉,所以3个信息框会自动弹出,数据说例如第1个信息框将包含詹姆斯,第2个标记,第3个等等。截至目前,如果我把它放在循环中并尝试添加它总是需要最后一个值
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());
}
答案 0 :(得分:0)
设置for循环内的信息框的选项和位置,而不是之后。然后在图钉中存储每个信息框的引用。执行pin.infobox = pinInfobox;
之类的操作然后在displayInfobox函数中,您可以通过执行以下操作来显示信息框:
e.target.infobox.setOptions({visible: true});
也就是说,如果你有很多图钉,最好有一个信息框并根据需要更新它。这是一篇博文,概述了这种方法:https://rbrundritt.wordpress.com/2011/10/13/multiple-pushpins-and-infoboxes-in-bing-maps-v7/