2013-03-13 10 views
0

Im делает веб-приложение в C# и ASP.NET MVC4. У меня возникла проблема с загрузкой карты на одной из моих страниц просмотра ... У меня есть карта на моей странице сведений, и вы переходите со страницы «Индекс» на страницу «Подробности».bing map не загружается

Это часть моего кода:

<div id='myMap' style="position:relative; width:400px; height:400px;"> 
</div> 
<div> 
    <input type="button" value="createWalkingRoute" onclick="createDirections();" /> 
</div> 
<div id='directionsItinerary'> </div> 


@section scripts{ 

<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 directionsManager; 
     var directionsErrorEventObj; 
     var directionsUpdatedEventObj; 

     function getMap() { 
      map = new Microsoft.Maps.Map(document.getElementById('myMap'), { credentials: 'mykey' }); 
     } 

     function createDirectionsManager() { 
      var displayMessage; 
      if (!directionsManager) { 
       directionsManager = new Microsoft.Maps.Directions.DirectionsManager(map); 
       displayMessage = 'Directions Module loaded\n'; 
       displayMessage += 'Directions Manager loaded'; 
      } 
      alert(displayMessage); 
      directionsManager.resetDirections(); 
      directionsErrorEventObj = Microsoft.Maps.Events.addHandler(directionsManager, 'directionsError', function (arg) { alert(arg.message) }); 
      directionsUpdatedEventObj = Microsoft.Maps.Events.addHandler(directionsManager, 'directionsUpdated', function() { alert('Directions updated') }); 
     } 

     function createWalkingRoute() { 
      if (!directionsManager) { createDirectionsManager(); } 
      directionsManager.resetDirections(); 
      // Set Route Mode to walking 
      directionsManager.setRequestOptions({ routeMode: Microsoft.Maps.Directions.RouteMode.walking }); 
      var seattleWaypoint = new Microsoft.Maps.Directions.Waypoint({ address: 'Seattle, WA' }); 
      directionsManager.addWaypoint(seattleWaypoint); 
      var redmondWaypoint = new Microsoft.Maps.Directions.Waypoint({ address: 'Redmond, WA', location: new Microsoft.Maps.Location(47.678561, -122.130993) }); 
      directionsManager.addWaypoint(redmondWaypoint); 
      // Set the element in which the itinerary will be rendered 
      directionsManager.setRenderOptions({ itineraryContainer: document.getElementById('directionsItinerary') }); 
      alert('Calculating directions...'); 
      directionsManager.calculateDirections(); 
     } 

     function createDirections() { 
      if (!directionsManager) { 
       Microsoft.Maps.loadModule('Microsoft.Maps.Directions', { callback: createWalkingRoute }); 
      } 
      else { 
       createWalkingRoute(); 
      } 
     } 

getMap(); 

    </script> 

} 

Когда вы идете первым идти на странице Детали карта не загружается. Однако, если страница обновляется, тогда карта загружается после. Так что для меня это какая-то проблема загрузки. Но, пробовав несколько часов, я абсолютно застрял. Может ли кто-нибудь помочь? спасибо

ответ

0

введите getMap() звонок в место, где он будет вызываться после загрузки страницы, например body onload event. Если вы используете jquery, $(document).ready().

+0

Bojin Li: спасибо за это, я пробовал оба из них, но карта все еще не загружается, пока страница не обновится. – archie

+0

Ваше решение объяснено Боджином Ли, на самом деле вы должны обратиться к iSDK здесь: http://www.bingmapsportal.com/isdk/ajaxv7 Ваш код выполняется до сценариев Bing Maps (добавлен из скрипта ссылка) была загружена на страницу (в браузере) –

Смежные вопросы