在javascript中使用for循环中的if语句时出错

时间:2015-07-02 20:53:58

标签: javascript if-statement for-loop

我编写了一个java脚本代码,用于计算三角形三边的行车距离,三角形由3个邮政编码组成。我想用三个距离进行简单的计算,例如a-b-c并显示结果。我得到null结果。我的代码在哪里出错?



function initialize() {
     var mapOptions = {
          zoom:      8,
          center:    new google.maps.LatLng(-34.397, 150.644),
          mapTypeId: google.maps.MapTypeId.ROADMAP
     };
     var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

     //INITIALIZE GLOBAL VARIABLES
     var zipCodesToLookup1 = new Array('31402', '30308', '30901', '31402');
     var output           = '<tr><th scope="col">From</th><th scope="col">To</th><th scope="col">Miles</th></tr>';
     var difference = "0";
     var totalDist = 0;
    // document.write(difference);
     //EXECUTE THE DISTANCE MATRIX QUERY
     var service = new google.maps.DistanceMatrixService();
     service.getDistanceMatrix({
          origins:      zipCodesToLookup1,
          destinations: zipCodesToLookup1,
          travelMode:   google.maps.TravelMode.DRIVING,
          unitSystem:   google.maps.UnitSystem.IMPERIAL
     }, function(response, status) {
          if(status == google.maps.DistanceMatrixStatus.OK) {
               var origins = response.originAddresses;
               var destinations = response.destinationAddresses;
               for(var i=0; i < origins.length-1; i++) {
                     var results = response.rows[i].elements;
                     output += '<tr><td>' + origins[i] + '</td><td>' + destinations[i+1] + '</td><td>' + results[i+1].distance.text + '</td></tr>';
                     if (var i = 0){
                     totalDist += results[i+1].distance.value;
                     }
                     else {
                     totalDist -= results[i+1].distance.value;
                     }

               }
              output += '<tr><td></td><td></td><td>'+(totalDist/1000*0.621371).toFixed(2)+ ' mi</td></tr>';


               document.getElementById('zip_code_output').innerHTML = '<table cellpadding="5">' + output + '</table>';
          }
     });
}

google.maps.event.addDomListener(window, "load", initialize);
&#13;
html, body, #map_canvas {
    height: 500px;
    width: 500px;
    margin: 0px;
    padding: 0px
}
&#13;
<div id="zip_code_output"></div>
<div id="map_canvas" style="border: 2px solid #3872ac;"></div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

在当前实现中,if语句中的变量与for循环中的变量不同。这可能不是你想要的。您应该从var声明中删除if关键字,就像我在下面所做的那样。如果变量应该是不同的,那么最好使用不同的名称。

             for(var i=0; i < origins.length-1; i++) {
                 var results = response.rows[i].elements;
                 output += '<tr><td>' + origins[i] + '</td><td>' + destinations[i+1] + '</td><td>' + results[i+1].distance.text + '</td></tr>';
                 if (i == 0){
                     totalDist += results[i+1].distance.value;
                 }
                 else {
                     totalDist -= results[i+1].distance.value;
                 }

           }

javascript中的变量范围可能很棘手。