2012-11-29 78 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對象上。