我試圖在同一頁上顯示多個bing地圖,每個地圖都有基於地理編碼顯示的指定位置。使用REST服務在頁面上初始化多個Bing圖與圖釘
我遇到的問題是這樣的......
- 當我展示一個地圖和與位置intialise它 - 這是 罰款。
- 當我運行同樣的動作連續多次到多個地圖的div不顯示 正確的 - 往往只是intialised最後一個地圖顯示 (有時是相當隨機的,雖然,有兩個被顯示的與位置 有時混合)。有趣的是,如果我在每次地圖初始化之間暫停一段時間,提醒一切都會正常工作(在我的示例中,只需取消註釋即可看到)。這似乎發生在FF,而不是IE由於我剛纔注意到的某些原因。
我構建了一個基於msdn example provided的單頁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地圖更好的方式來做到這一點 - 這將是非常值得讚賞的,我將永遠在你的債務債務。謝謝。
非常感謝您的回答,我今天將研究如何使用這種方法,並讓您瞭解它的發展方向! – Geoffry