0

我已经在这里找到了一些关于 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; }
}

因为我想获取每个列表项的所有纬度和经度属性以将它们绘制为谷歌地图控件中的折线,所以我在地图的初始化中添加了下面的代码。我只是不知道如何遍历对象。

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

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 对象并获取属性值来帮助我吗?

4

2 回答 2

0

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

但是,我会尽力提供帮助:

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

前面的循环遍历 html 文档中的元素。

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

前面的循环遍历一个 javascript 对象。

于 2012-11-29T11:42:36.883 回答
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);
    });
于 2012-11-29T20:34:28.303 回答