0

我正在构建一个示例 MVC4 Web 应用程序,我的目标是学习和掌握 Bing Maps API 的使用,但恐怕进展不顺利。

我正在关注本教程,但是当我重新加载页面时,我没有得到地图,地图应该显示的地方,我只看到空白背景颜色!即使在获得 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>
4

1 回答 1

1

在您的实现中需要考虑几件事。

  1. 将脚本引用添加到 Bing Maps API
  2. 在您的网页中添加 HTML 元素和所需信息
  3. 添加加载事件并触发您的 getMap() 方法(使用在主体上设置的 onload 事件或通过代码动态)

请参阅 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>
于 2013-02-17T20:56:10.783 回答