如何将多个latlon值从数组传递到bing贴图

时间:2014-12-05 06:45:30

标签: javascript arrays bing-maps

这里我试图在bing地图上获得多个图钉,所以我有一个纬度和放大器阵列经度,我将这些值从数组传递到bing maps lat& lon变量。

当我使用这些变量在地图上放置图钉时,我无法在地图上看到图钉。 我该如何解决这个问题?

这就是我所做的

代码:

function showLoc()
{
    map = new Microsoft.Maps.Map(document.getElementById('myMap'), {credentials: bkey});
    map.setView({ zoom: 12, center: new Microsoft.Maps.Location(lat,lon) })
    map.entities.clear(); 

    var pushpin= new Microsoft.Maps.Pushpin(map.getCenter(), null);         
    map.entities.push(pushpin); 

    var index;
    var latlon = ["12.978955,77.574838", "12.981472,77.619214", "12.980332,77.617265", "13.011561,77.606354", "12.975496,77.556826", "12.97518,77.570991", "12.971599,77.594563", "12.985405,77.525386"];
    for (index = 0; index < latlon.length; index++) {
    var laton = latlon[index];
    pushpin.setLocation(new Microsoft.Maps.Location(laton)); 
    }
}  

1 个答案:

答案 0 :(得分:2)

您必须为每个位置创建一个图钉,否则您只需继续移动相同的图钉。您还必须将字符串值解析为坐标解析(两个数字)。将代码修改为以下内容:

function showLoc()
{
    map = new Microsoft.Maps.Map(document.getElementById('myMap'), {credentials: bkey});
    map.setView({ zoom: 12, center: new Microsoft.Maps.Location(lat,lon) })
    map.entities.clear(); 

    var latlon = ["12.978955,77.574838", "12.981472,77.619214", "12.980332,77.617265", "13.011561,77.606354", "12.975496,77.556826", "12.97518,77.570991", "12.971599,77.594563", "12.985405,77.525386"];
    for (var i = 0; i < latlon.length; i++) {
        var vals = latlon[index].split(',');

        if(vals.length >= 2){
            var pushpin = new Microsoft.Maps.Pushpin(new Microsoft.Maps.Location(parseFloat(vals[0]), parseFloat(vals[1])));         
            map.entities.push(pushpin); 
        }
    }
}