在 Bing 地图上的多个图钉上弹出信息框,图钉可点击但信息框不显示
Pop-up Infoboxes on multiple pushpins on a Bing Map, pins clickable but infobox not showing
我就这样兜兜转转。我正在尝试使用显示在单击的图钉旁边的单个信息框添加多个图钉。我可以让图钉显示并可点击,但信息框不显示。
我哪里错了?
在此先感谢您的帮助!
代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0&mkt=de-de" type="text/javascript" charset="UTF-8"></script>
</head>
<body onload="init()">
<script type='text/javascript' src='http://www.bing.com/api/maps/mapcontrol?callback=GetMap&key=AhrgxvPMzaWNe4oDZqelNzT-SoWigBK7H8cSBqZ2jLXpq4O9IcQ1YktvHhqT7_8F' async defer></script> "
<div id="map" style="position: relative; width: 800px; height: 300px;"></div>
<script type="text/javascript">
function init(){
// Initialize the map
var map = new Microsoft.Maps.Map(
document.getElementById("map"),
{
credentials: "My Bing Maps API Key",
mapTypeId: Microsoft.Maps.MapTypeId.road
}
);
var infobox = new Microsoft.Maps.Infobox(map.getCenter(), {
visible: false
});
infobox.setMap(map);
// Create a collection to store multiple pins
var pins = new Microsoft.Maps.EntityCollection();
// Create pins
for (var i = 0; i < 5; i++){
var position = new Microsoft.Maps.Location(53 + i, -1);
// Creates a Pushpin
var pin = new Microsoft.Maps.Pushpin(position, {title: 'pin' + i} );
pin.metadata = {
title: 'Pin ' + i,
description: 'Description for pin ' + i
};
Microsoft.Maps.Events.addHandler(pin, 'click', pushpinClicked);
// Adds the pin to the collection instead of adding it directly to the Map
pins.push(pin);
}
// Adds all pins at once
map.entities.push(pins);
}
function pushpinClicked(e) {
//Make sure the infobox has metadata to display.
if (e.target.metadata) {
//Set the infobox options with the metadata of the pushpin.
infobox.setOptions({
location: e.target.getLocation(),
title: e.target.metadata.title,
description: e.target.metadata.description,
visible: true
});
}
}
</script>
</body>
</html>
您在脚本标记中引用了 Bing Maps V7。那个版本在几年前就被弃用了。它在幕后重定向到 v8,但这不是 100% 向后兼容,可能会导致问题。这是 V8 上的相同示例 运行:https://bingmapsv8samples.azurewebsites.net/#Infobox_MultiplePushpins
我就这样兜兜转转。我正在尝试使用显示在单击的图钉旁边的单个信息框添加多个图钉。我可以让图钉显示并可点击,但信息框不显示。
我哪里错了?
在此先感谢您的帮助!
代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0&mkt=de-de" type="text/javascript" charset="UTF-8"></script>
</head>
<body onload="init()">
<script type='text/javascript' src='http://www.bing.com/api/maps/mapcontrol?callback=GetMap&key=AhrgxvPMzaWNe4oDZqelNzT-SoWigBK7H8cSBqZ2jLXpq4O9IcQ1YktvHhqT7_8F' async defer></script> "
<div id="map" style="position: relative; width: 800px; height: 300px;"></div>
<script type="text/javascript">
function init(){
// Initialize the map
var map = new Microsoft.Maps.Map(
document.getElementById("map"),
{
credentials: "My Bing Maps API Key",
mapTypeId: Microsoft.Maps.MapTypeId.road
}
);
var infobox = new Microsoft.Maps.Infobox(map.getCenter(), {
visible: false
});
infobox.setMap(map);
// Create a collection to store multiple pins
var pins = new Microsoft.Maps.EntityCollection();
// Create pins
for (var i = 0; i < 5; i++){
var position = new Microsoft.Maps.Location(53 + i, -1);
// Creates a Pushpin
var pin = new Microsoft.Maps.Pushpin(position, {title: 'pin' + i} );
pin.metadata = {
title: 'Pin ' + i,
description: 'Description for pin ' + i
};
Microsoft.Maps.Events.addHandler(pin, 'click', pushpinClicked);
// Adds the pin to the collection instead of adding it directly to the Map
pins.push(pin);
}
// Adds all pins at once
map.entities.push(pins);
}
function pushpinClicked(e) {
//Make sure the infobox has metadata to display.
if (e.target.metadata) {
//Set the infobox options with the metadata of the pushpin.
infobox.setOptions({
location: e.target.getLocation(),
title: e.target.metadata.title,
description: e.target.metadata.description,
visible: true
});
}
}
</script>
</body>
</html>
您在脚本标记中引用了 Bing Maps V7。那个版本在几年前就被弃用了。它在幕后重定向到 v8,但这不是 100% 向后兼容,可能会导致问题。这是 V8 上的相同示例 运行:https://bingmapsv8samples.azurewebsites.net/#Infobox_MultiplePushpins