2013-02-16 29 views
0

我正在構建一個示例MVC4 Web應用程序,我的目標是學習和掌握Bing Maps API的工作,但恐怕它不好。Bing地圖不顯示在Localhost MVC應用程序

我正在關注this tutorial,但是當我重新加載我的頁面時,我沒有看到地圖,地圖應該顯示,我只看到空白背景色!即使獲得了API密鑰,並將其放入我的頁面上的腳本!沒有地圖顯示!

這裏是我的腳本代碼:

<script type="text/javascript"> 
var map = null; 

function getMap() 
{ 
    var boundingBox = Microsoft.Maps.LocationRect.fromLocations(new Microsoft.Maps.Location(47.618594, -122.347618), new Microsoft.Maps.Location(47.620700, -122.347584), new Microsoft.Maps.Location(47.622052, -122.345869)); 
    map = new Microsoft.Maps.Map(document.getElementById('myMap'), { credentials: 'MyKeyGoesHere', bounds: boundingBox }); 
} 
</script> 

任何幫助將不勝感激。

回答

1

在您的實施中需要考慮幾件事情。

  1. 腳本引用添加到Bing地圖API
  2. 添加HTML元素在網頁中
  3. 所需的信息添加加載事件和消防您的GetMap()方法(使用設置onload事件身體或代碼dynamicaly)

請參閱MSDN幫助您在第一次執行: http://msdn.microsoft.com/en-us/library/gg427624.aspx

這裏是一個樣本靜態頁:

<!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" /> 
    <title></title> 
    <script type="text/javascript" charset="UTF-8" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0"> 
    </script> 
    <script type="text/javascript"> 
     var map = null; 

     function getMap() { 
      var boundingBox = Microsoft.Maps.LocationRect.fromLocations(
       new Microsoft.Maps.Location(47.618594, -122.347618), 
       new Microsoft.Maps.Location(47.620700, -122.347584), 
       new Microsoft.Maps.Location(47.622052, -122.345869)); 

      map = new Microsoft.Maps.Map(
       document.getElementById('myMap'), 
       { 
        credentials: 'YOURKEY', 
        bounds: boundingBox 
       }); 
     } 

    </script> 
</head> 
<body onload="getMap();"> 
    <div id="myMap" style="position: relative; width: 800px; height: 600px;"> 
    </div> 
</body> 
</html> 
+0

謝謝尼古拉斯。我錯過了onload =「getMap();」在身體div上。它現在工作:) – Ciwan 2013-02-18 08:33:30