用javasctipt解析json坐标

时间:2016-02-10 19:56:55

标签: javascript json

我需要找到一个最接近的坐标,并安排最接近的城市。 在控制台日志中,我需要有这样的:8 city,63.50590523722971; 5 city,76.32168761236873 我认为coordonates是好的,但我如何附加城市属性" city"结果[i]并用它来获取。

var locationcity = [
  {"city":"1 City","value":"61,90"},
  {"city":"2 City","value":"34,97"},
  {"city":"3 City","value":"21,63"},
  {"city":"4 City","value":"19,84"},
  {"city":"5 City","value":"0,81"},
  {"city":"6 City","value":"6,76"},
  {"city":"7 City","value":"43,64"},
  {"city":"8 City","value":"18,64"},
  {"city":"9 City","value":"10,61"},
]

function findClosest(locationcity){
    var result = [];
    for(var i=0; i < locationcity.length; i++){
        for(var j = 1; j < locationcity.length-1 ; j++){
            var x, y , r1 , r2;
            var id = locationcity[i].id;
            var coordinate_x = locationcity[i].value.split(",");
            var coordinate_y = locationcity[j].value.split(",");
            x = coordinate_x[0] - coordinate_y[0];
            y = coordinate_x[1] - coordinate_y[1];
            r1 = Math.pow(x, 2);
            r2 = Math.pow(y, 2);
            result[i] = Math.sqrt(r1 + r2);
        }
    }
   var closest = result.sort();
   $(closest).each(function(){
     var _this = $(this);
     console.log(_this);
   });
}
findClosest(locationcity);

由于

2 个答案:

答案 0 :(得分:0)

sort函数可以将可选函数作为参数: http://www.w3schools.com/jsref/jsref_sort.asp

所以不要只存储距离,而是像这样存储城市:

var closest = result.sort(function (a, b) {
   return a['distance'] - b['distance'];
});

然后在排序时提供排序功能:

<body>
    <section class="quote-box row">
        <div class="row">
            <h2 id="quote"><i class="ion-quote quotes-icon"></i>Words have meanings! Or so people tell me I'm not sure!</h2></div>
        <div class="row"><h4 class="author">- Zack Fanning</h4></div>
        <div class="row buttons">
            <button id="twitter-button"><a href="https://twitter.com/intent/tweet"><i class="ion-social-twitter twtter-button-icon"></i></a></button>
            <button id="new-quote">New quote</button>
        </div>
    </section>
    <section class="created-by row">
    <h4>By Zack Fanning</h4>
    </section>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script src="resources/js/script.js"></script>
</body>


$(document).ready(function(){

/* QUOTE ARRAY */
var array = ["a","b","c","d","e","f","g","h","i","j"];

/* NEW QUOTE BUTTON */

$("#new-quote").click(function(){
   document.getElementById("#new-quote").style.backgroundColor = "blue";
});  

};

答案 1 :(得分:0)

我真的不明白你是否想要显示城市到城市的距离并对它们进行排序,但这就是我要做的,很快,可能需要重构;)

function findClosest(locationcity) {

var res = new Array();
for(var i=0; i < locationcity.length; i++) {

        var j = i + 1; // This is important to avoid duplications : ex.: 1 to 2 and 2 to 1
        while(j < locationcity.length ) {

            var x, y , r1 , r2;
            var cityA = locationcity[i].city;
            var cityB = locationcity[j].city;
            var coordinate_x = locationcity[i].value.split(",");
            var coordinate_y = locationcity[j].value.split(",");
            x = coordinate_x[0] - coordinate_y[0];
            y = coordinate_x[1] - coordinate_y[1];
            r1 = Math.pow(x, 2);
            r2 = Math.pow(y, 2);

            var distance_title  = cityA + " to " + cityB;
            var distance_value   = Math.sqrt(r1 + r2);

            res.push({
              "name" : distance_title,
              "dist" : distance_value
            });
            j++;
          }
     }

     var closest = res.sort(function (a, b) {
         return a['dist'] - b['dist'];
     });

     for(var key in closest) {
        console.log(closest[key]);
     }
}

findClosest(locationcity);

但是再一次,这是按照它们之间的距离对城市进行分类! 希望它对你有用:)

最好的问候。