0

我在 C# 和 ASP.NET MVC4 中做一个 Web 应用程序。我在我的一个视图页面上加载地图时遇到问题...我的详细信息页面上有地图,您从索引页面转到详细信息页面。

这是我的一些代码:

<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>

 }

当您首先进入详细信息页面时,地图不会加载。但是,如果随后刷新页面,则地图会在之后加载。所以对我来说,这是某种加载问题。但在尝试了几个小时后,我完全卡住了。任何人都可以帮忙吗?谢谢

4

1 回答 1

0

getMap()调用放在页面加载后将被调用的某个地方,例如body onload 事件。如果您使用的是 jquery,则$(document).ready()

于 2013-03-14T09:16:41.177 回答