-2

看代码。感谢大家的帮助。非常感谢您的宝贵反馈。但它没有帮助。我真的很感谢你。

   
      $.get("http://nominatim.openstreetmap.org/search?q=pekin,+china&format=json&polygon=1&addressdetails=1").done(function(data)
        {
            var aJsonData = new Array();
            var iBiggest = 0;

            aJsonData = JSON.parse(data);
            aData = aJsonData;

            for(var i=0; i < aData.length; i++)
            {
                if(i != 0)
                {
                    if((aData[i].polygonpoints.length) > (aData[iBiggest].polygonpoints.length))
                    {
                        iBiggest = i;
                    }
                }
            }

            alert(iBiggest);

            for(var j=0; j < aData[iBiggest].polygonpoints.length; j++)
            {
                //alert(aData[iBiggest].polygonpoints[j]);
            }
        });
   
4

2 回答 2

1

你的 for 循环是错误的,数组索引将从 0 开始到 length - 1 所以i <= aData.length是错误的。

所以循环应该是

$
.get("http://nominatim.openstreetmap.org/search?q=london,+england&format=json&polygon=1&addressdetails=1")
.done(function(data) {

    var iBiggest = 0;

    for (var i = 1; i < data.length; i++) {
        if ((data[i].polygonpoints.length) > (data[iBiggest].polygonpoints.length)) {
            iBiggest = i;
        }
    }

    // this is not working
    alert(iBiggest);

    for (var j = 0; j < data[iBiggest].polygonpoints.length; j++) {
        // alert(aData[iBiggest].polygonpoints[j]);
    }
}, 'json');

演示:小提琴

于 2013-05-08T08:16:35.190 回答
0

我解决了。

   
      $.getJSON("http://nominatim.openstreetmap.org/search?q=pekin,+china&format=json&polygon=1&addressdetails=1", function(data)
        {
            var iBiggest = 0;

            for(var i = 1; i < data.length; i++)
            {
                if(data[i].polygonpoints != 'undefined' && data[i].polygonpoints)
                {
                    if((data[i].polygonpoints.length) > (data[iBiggest].polygonpoints.length))
                    {
                        iBiggest = i;
                    }
                }
            }

            alert(iBiggest);

            for(var j = 0; j < data[iBiggest].polygonpoints.length; j++)
            {
                alert(data[iBiggest].polygonpoints[j]);
            }
        });
   
于 2013-05-10T13:00:14.037 回答