【问题标题】:Giving each Google Distance Matrix result an id给每个谷歌距离矩阵结果一个 id
【发布时间】:2013-12-12 21:39:25
【问题描述】:

我正在使用谷歌距离矩阵https://developers.google.com/maps/documentation/distancematrix/ 计算一些运费。当它在 for 循环中输出结果时,每次都会将它们放到一个新行上,所以我最终得到:

1
25
26
1

是否可以将每个结果存储在一个变量中,以便我可以在代码中的其他地方调用每个单独的结果,以便进行一些数学计算以计算成本等,所以.....

$result1
$result2
$result3
$result4

稍后我想做诸如 result1 * result3 = etc 之类的事情。

        <script>

          var map;
          var geocoder;
          var bounds = new google.maps.LatLngBounds();
          var markersArray = [];

          var base = new google.maps.LatLng(55.930385, -3.118425);
          var start = 'Greenwich, England';
          var destinationA = 'Stockholm, Sweden';
          var end = new google.maps.LatLng(55.930385, -3.118425);

          var destinationIcon = 'https://chart.googleapis.com/chart?chst=d_map_pin_letter&chld=D|FF0000|000000';
          var originIcon = 'https://chart.googleapis.com/chart?chst=d_map_pin_letter&chld=O|FFFF00|000000';

          function initialize() {
            var opts = {
              center: new google.maps.LatLng(55.53, 9.4),
              zoom: 10,
              mapTypeId: google.maps.MapTypeId.ROADMAP
            };
            map = new google.maps.Map(document.getElementById('map'), opts);
            geocoder = new google.maps.Geocoder();
            calculateDistances();
          }

          function calculateDistances() {
            var service = new google.maps.DistanceMatrixService();
            service.getDistanceMatrix(
              {
                origins: [base],
                destinations: [start, end],
                travelMode: google.maps.TravelMode.DRIVING,
                unitSystem: google.maps.UnitSystem.IMPERIAL,
                avoidHighways: false,
                avoidTolls: false
              }, callback);
          }

          function callback(response, status) {
            if (status != google.maps.DistanceMatrixStatus.OK) {
              alert('Error was: ' + status);
            } else {
              var origins = response.originAddresses;
              var destinations = response.destinationAddresses;
              var outputDiv = document.getElementById('outputDiv');
              outputDiv.innerHTML = '<table border="1">';
              deleteOverlays();
              var stringArray = ['$runinPickup','$runinDestination'];
              var htmlString = '<table border="1">';
              for (var i = 0; i < origins.length; i++) {
                var results = response.rows[i].elements;
                addMarker(origins[i], false);
                for (var j = 0; j < results.length; j++) {
                  addMarker(destinations[j], true);
                  htmlString += '<tr><td>'+stringArray[j]+'</td><td>' + results[j].distance.text +'</td></tr>';
                  outputDiv.innerHTML += '<tr><td>'+stringArray[j]+'</td><td>' + results[j].distance.text +'</td></tr>';
                }
              }
              htmlString += '</table>';
              // outputDiv.innerHTML += '</table>';
              outputDiv.innerHTML = htmlString;
              // alert(htmlString);
            }
          }

          function addMarker(location, isDestination) {
            var icon;
            if (isDestination) {
              icon = destinationIcon;
            } else {
              icon = originIcon;
            }
            geocoder.geocode({'address': location}, function(results, status) {
              if (status == google.maps.GeocoderStatus.OK) {
                bounds.extend(results[0].geometry.location);
                map.fitBounds(bounds);
                var marker = new google.maps.Marker({
                  map: map,
                  position: results[0].geometry.location,
                  icon: icon
                });
                markersArray.push(marker);
              } else {
                alert('Geocode was not successful for the following reason: '
                  + status);
              }
            });
          }

          function deleteOverlays() {
            if (markersArray) {
              for (i in markersArray) {
                markersArray[i].setMap(null);
              }
              markersArray.length = 0;
            }
          }

        </script>

【问题讨论】:

    标签: javascript google-maps-api-3


    【解决方案1】:

    嗯,这应该不会太难。输出值的位置,例如

    htmlString += '<tr><td>'+stringArray[j]+'</td><td>' + results[j].distance.text +'</td></tr>';
    

    只需添加一行将该值分配给变量即可。我倾向于将它们添加到一个数组中,您可以稍后循环。

    arrResults.push(results[j].distance.text);
    

    如果您确信自己始终知道每个值是什么,那么您可以简单地引用它们,例如 arrResults[0] * arrResults[2]

    【讨论】:

    • 对不起,我为那个添加了错误的行,它应该是 html += parseInt(elements[j].distance.text) + "
      ";
    • 如何将它添加到数组中?
    • array.push(parseInt(elements[j].distance.text));
    猜你喜欢
    • 2017-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-12
    • 2016-12-19
    相关资源
    最近更新 更多