0

我有以下硬编码的 JSON:

 var myData = [
                  {"Identifier":1,"Naam":"Van Der Valk","Adres":"Europaweg 218","Postcode":"1238AC","Plaats":"Zoetermeer","Longitude":"4.48822","Latitude":"52.06258", "Status":"Laadpunten Beschikbaar", "lunch":"true", "diner":"true", "meet":"true", "wifi":"true"},
                  {"Identifier":2,"Naam":"NOT Given","Adres":"NOT Given 1","Postcode":"0000LL","Plaats":"Rotterdam","Longitude":"0.00000","Latitude":"0.00000", "lunch":"false"},
                  {"Identifier":3,"Naam":"NOT Given","Adres":"NOT Given 6","Postcode":"0000LL","Plaats":"Rotterdam","Longitude":"0.00000","Latitude":"0.00000", "lunch":"false"},
                  {"Identifier":4,"Naam":"NOT Given","Adres":"NOT Given 1","Postcode":"0000LL","Plaats":"Den Haag","Longitude":"0.00000","Latitude":"0.00000", "lunch":"false"},
                  {"Identifier":5,"Naam":"plek b met lunch en diner","Adres":"NOT Given 218","Postcode":"0000LL","Plaats":"Zoetermeer","Longitude":"0.00000","Latitude":"0.00000", "lunch":"true", "diner":"true", "wifi":"true"}
                  ];

我在我的 ios phonegap 应用程序中使用的这个 json .. Json 中的每一行代码都是荷兰的一个点.. 我正在尝试构建一个函数来计算手机所在位置与 json 的纬度和经度之间的距离..最终这些json代码需要进入一个有序列表,其中距离也是从小到大..

对于距离测量,我使用了这个:

 function haversine() {
            $.each(myData, function(index, element) {
                   var R = 6371; // earth's mean radius in km

                   var dLat  = rad(element.Latitude - lat1);


                   var dLong = rad(element.Longitude - long1);

                   var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
                   Math.cos(rad(lat1)) * Math.cos(rad(element.Latitude)) * Math.sin(dLong/2) * Math.sin(dLong/2);
                   var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
                   var d = R * c;

                   //var element = document.getElementById('afstand');
                   //element.innerHTML = 'afstand: ' + d.toFixed(3);

                   $('.greenfluxlist').append('<li id="' + element.Identifier + '"><a href="Details.html?id=' + element.Identifier + '" rel="external">' + element.Naam + ' ' + element.Plaats +  '<p> <br/>' + element.Adres + ',<br/> ' + element.Postcode + ' ' + element.Plaats + '<br/> Status: ' + element.Status +  '</p> <span class="ui-li-count">' + d.toFixed(3) + '</span></a></li>');
                   });

            $('.greenfluxlist').trigger('create'); 
            $('.greenfluxlist').listview('refresh');

            //return d.toFixed(3);

        }

我现在的问题是,如何构建有序列表,是否可以将所有内容放入数组中,然后循环遍历它或其他东西,我认为必须有一个简单的解决方案,但我不知道。

4

1 回答 1

1

要对数组进行排序,请使用sort方法。

您应该先计算距离,然后对数组进行排序,然后生成列表。像这样的东西:

function calculateDistance(element)
{
    var R = 6371; // earth's mean radius in km

    var dLat  = rad(element.Latitude - lat1);
    var dLong = rad(element.Longitude - long1);

    var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
    Math.cos(rad(lat1)) * Math.cos(rad(element.Latitude)) * Math.sin(dLong/2) * Math.sin(dLong/2);
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    return R * c;
}

function generateList(data)
{
    for(var i=0; i<data.lenght; i++)
    {
        var element = data[i];
        //var element = document.getElementById('afstand');
        //element.innerHTML = 'afstand: ' + element.Distance.toFixed(3);

        $('.greenfluxlist').append('<li id="' + element.Identifier + '"><a href="Details.html?id=' + element.Identifier + '" rel="external">' + element.Naam + ' ' + element.Plaats +  '<p> <br/>' + element.Adres + ',<br/> ' + element.Postcode + ' ' + element.Plaats + '<br/> Status: ' + element.Status +  '</p> <span class="ui-li-count">' + element.Distance.toFixed(3) + '</span></a></li>');
    }
}

for(var i=0; i<myData.length; i++)
{
    myData[i].Distance = calculateDistance(myData[i]);
}

myData.sort(function(a,b) {
    return a.Distance > b.Distance;
});

generateList(myData);
于 2012-10-16T09:51:05.080 回答