我正在尝试在同一页面上显示多个 bing 地图,每个地图都有一个基于地理编码显示的指定位置。
我遇到的问题是这个...
- 当我显示一张地图并用一个位置初始化它时 - 这很好。
- 当我对多个地图 div 连续多次运行相同的操作时,它无法正确显示 - 通常只显示最后一张初始化的地图(但有时它是非常随机的,显示两个,有时位置混淆)。
- 有趣的是,如果我在每次地图初始化之间暂停并显示警报,一切正常(在我下面的示例中,只需取消注释警报,您就可以看到)。由于我刚刚注意到的某种原因,这似乎发生在 FF 而不是 IE 中。
我基于提供的msdn 示例构建了一个单页 html 示例。整个标记在下面,复制并保存为 html 文件,它应该如上所述工作。我只是为了便于理解和更好地与我的问题沟通而将其搅动。
<!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 = null;
var mapId = '';
var geocode = ''
function InistionaliseMaps()
{
//alert('1');
InistialiseMap('mapDiv1', 'London');
//alert('2');
InistialiseMap('mapDiv2', 'Barcelona');
//alert('3');
InistialiseMap('mapDiv3', 'Auckland');
//alert('4');
InistialiseMap('mapDiv4', 'New York');
//alert('5');
InistialiseMap('mapDiv5', 'Amsterdam');
}
function InistialiseMap(mId, gc)
{
mapId = mId;
geocode = gc;
GetMap();
ClickGeocode();
}
function GetMap()
{
// Initialize the map
map = new Microsoft.Maps.Map(document.getElementById(mapId),{credentials:"Auy_rZ68nbxt5cE4EYg7o1pFD3IpEg6sgNNWC8RyP04f12t9GSf0YQzlnBmgyJV3", mapTypeId:Microsoft.Maps.MapTypeId.road});
}
function ClickGeocode(credentials)
{
map.getCredentials(MakeGeocodeRequest);
}
function MakeGeocodeRequest(credentials)
{
var geocodeRequest = "http://dev.virtualearth.net/REST/v1/Locations?query=" + encodeURI(geocode) + "&output=json&jsonp=GeocodeCallback&key=" + credentials;
CallRestService(geocodeRequest);
}
function GeocodeCallback(result)
{
alert("Found location: " + result.resourceSets[0].resources[0].name);
if (result &&
result.resourceSets &&
result.resourceSets.length > 0 &&
result.resourceSets[0].resources &&
result.resourceSets[0].resources.length > 0)
{
// Set the map view using the returned bounding box
var bbox = result.resourceSets[0].resources[0].bbox;
var viewBoundaries = Microsoft.Maps.LocationRect.fromLocations(new Microsoft.Maps.Location(bbox[0], bbox[1]), new Microsoft.Maps.Location(bbox[2], bbox[3]));
map.setView({ bounds: viewBoundaries});
// Add a pushpin at the found location
var location = new Microsoft.Maps.Location(result.resourceSets[0].resources[0].point.coordinates[0], result.resourceSets[0].resources[0].point.coordinates[1]);
var pushpin = new Microsoft.Maps.Pushpin(location);
map.entities.push(pushpin);
}
}
function CallRestService(request)
{
var script = document.createElement("script");
script.setAttribute("type", "text/javascript");
script.setAttribute("src", request);
document.body.appendChild(script);
}
</script>
</head>
<body onload="InistionaliseMaps();">
<div id='mapDiv1' style="position:relative; width:400px; height:400px;"></div>
<div id='mapDiv2' style="position:relative; width:400px; height:400px;"></div>
<div id='mapDiv3' style="position:relative; width:400px; height:400px;"></div>
<div id='mapDiv4' style="position:relative; width:400px; height:400px;"></div>
<div id='mapDiv5' style="position:relative; width:400px; height:400px;"></div>
</body>
</html>
我相信这可能是因为页面加载速度过快并且对 REST 服务的连续调用重叠,或者 REST 服务中使用的对象在下一次调用或类似情况之前没有被销毁。老实说,我真的有点困惑。
如果有人知道这里可能发生了什么,解决问题的方法或通过 Bing 地图解决问题的更好方法 - 将不胜感激,我将永远在你的堆栈债务中。谢谢。