必应地图没有'当我向函数添加另一个参数时,t显示

Bing Map doesn't display when I add another parameter to my function

本文关键字:另一个 添加 函数 参数 显示 地图      更新时间:2023-09-26

我正在从数据库中加载经度和纬度,我想为我的函数添加一个额外的字段,并从数据库中传入其他值,以在我点击pin时显示,它只使用纬度和经度作为函数AddData(纬度,经度)的参数,效果很好,但一旦我添加另一个值,如AddData(经度,纬度,动物园名称),我的地图就不再显示了。我不确定我做错了什么,如果这是一个简单的错误,我很抱歉,但我只是习惯了javascript。

<script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0"></script>
<script type="text/javascript">
var map = null, infobox, dataLayer;
function GetMap() {
    // Initialize the map
    map = new Microsoft.Maps.Map(document.getElementById("myMap"),
               { credentials: "API Bing Key" });
    dataLayer = new Microsoft.Maps.EntityCollection();
    map.entities.push(dataLayer);
    var infoboxLayer = new Microsoft.Maps.EntityCollection();
    map.entities.push(infoboxLayer);
    infobox = new Microsoft.Maps.Infobox(new Microsoft.Maps.Location(0, 0), { visible: false, offset: new Microsoft.Maps.Point(0, 20) });
    infoboxLayer.push(infobox);
    @foreach (var item in Model)
       {
              @: AddData(@item.latitude,@item.longitude,@item.ZooName);
              //till here
                }
}
function AddData(latitude, longitude,ZooName) {
    var pin = new Microsoft.Maps.Pushpin(new Microsoft.Maps.Location(latitude, longitude));
    pin.Title = latitude.toString();
    pin.Description = latitude.toString();
    Microsoft.Maps.Events.addHandler(pin, 'click', displayInfobox);
    dataLayer.push(pin);
}
function displayInfobox(e) {
    if (e.targetType == 'pushpin') {
        infobox.setLocation(e.target.getLocation());
        infobox.setOptions({ visible: true, title: e.target.Title, description: e.target.Description });
    }
}
</script>
<body onload="GetMap();">
<div id='myMap'></div>
</body>

向函数添加额外的参数不会导致映射停止工作。我怀疑在访问@item.ZooName值时发生了错误。我猜这是一个字符串,它可能需要用引号括起来。您应该在浏览器控制台中看到一个错误。因此,该错误可能会阻止加载映射的代码启动,因此永远不会加载映射。