我已经在这里找到了一些关于javascript对象的文章,但我似乎还没有掌握它。我将JSON字符串转换为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; }
}
因为我希望获得每个列表项的所有纬度和经度属性,以便在Google地图控件中将它们绘制为多段线,所以我在地图的初始化中添加了以下代码。我只是不知道如何在对象中循环。
到目前为止,我的代码如下。
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对象并获取属性值吗?
发布于 2012-11-29 20:34:28
假设您使用的是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);
});
发布于 2012-11-29 11:42:37
我怀念关于html文档的文档结构的信息。
但是,我将尝试提供帮助:
$("#rootitemid").children().each(function(){
var value= $(this).html();
//..
});
前面的循环遍历html文档中的元素。
for(key in objs){
var value= objs[key];
//..
}
前面的循环遍历了一个javascript对象。
https://stackoverflow.com/questions/13624693
复制相似问题