2012-11-29 77 views
0

我已经在JavaScript对象上找到了一些文章但是我似乎还没有弄明白这一点。我将A JSON字符串转换为javascript对象,如下所示。通过JavaScript对象循环绘制多段线谷歌地图

public class LinePoint { 
    public string Latitude { get; set; } 
    public string Longitude { get; set; } 
    public int Pointnumber { get; set; } 
} 

public class PolyLine { 
    public List<LinePoint> LinePoints { get; set; } 
    public int PolyLineNumBer { get; set; } 
} 

public class RootObject { 
    public List<PolyLine> PolyLines { get; set; } 
} 

因为我想每个列表项的所有纬度和经度属性来绘制它们作为我添加以下代码到地图的初始化一个谷歌地图控制折线。我只是不知道如何循环对象。

这是我的代码到目前为止。

var line = JSON.parse(document.getElementById('hdfPolyLines').value); 
var path = []; 

$.each(/* Listitem in rootobject */) { 
    // Parse the array of LatLngs into Gmap points 
    for(/* each latlong in the list of listitems */){ 
     path.push(new google.maps.LatLng(// Latitude en Longitude properties from listitems */)); 
    } 
    var polyline = new google.maps.Polyline({ 
     path: path, 
     strokeColor: '#ff0000', 
     strokeOpacity: 1.0, 
     strokeWeight: 3 
    }); 

    polyline.setMap(map); 
}); 

有人能告诉我如何通过javascript对象循环并获取属性值吗?

+1

该代码看起来不像javascript,它看起来像java。 – geocodezip

+0

这不是Java,它是C# –

回答

0

假设您使用jQuery,您可以使用$ .each作为循环。这样的东西应该工作:

var line = JSON.parse(document.getElementById('hdfPolyLines').value); 

    $.each(line.PolyLines, function(polyLineIndex, polyLineValue) { 

     var path = Array(); 

     $.each(polyLineValue.LinePoints, function (linePointIndex, linePointValue) { 

      path.push(new google.maps.LatLng(linePointValue.Latitude, linePointValue.Longitude)); 
     }); 

     var polyline = new google.maps.Polyline({ 
      path: path, 
      strokeColor: '#ff0000', 
      strokeOpacity: 1.0, 
      strokeWeight: 3 
     }); 

     polyline.setMap(map); 
    }); 
+0

工程就像一个魅力。 Thanx! – Danny

0

我错过了关于html文档的文档结构的信息。

不过,我会尽力帮助:

$("#rootitemid").children().each(function(){ 
    var value= $(this).html(); 
    //.. 
}); 

以前的循环在HTML文档中的元素。

for(key in objs){ 
    var value= objs[key]; 
    //.. 
} 

上一个循环在javascript对象上。