【发布时间】:2020-04-09 16:44:41
【问题描述】:
我在这个圈子里转来转去。我正在尝试添加几个带有单个信息框的图钉,该信息框显示在单击的图钉旁边。我可以让图钉显示并可点击,但信息框没有显示。
我哪里错了?
提前感谢您的帮助!
代码如下:
<!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>
【问题讨论】:
标签: javascript html bing-maps