为每个Google Distance Matrix结果提供一个ID

时间:2013-12-12 08:50:36

标签: javascript google-maps-api-3

我正在使用Google距离矩阵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>

1 个答案:

答案 0 :(得分:1)

那不应该太难。输出值的位置,例如

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

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

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

如果您确信自己总是知道每个值是什么,那么您可以简单地将它们称为arrResults[0] * arrResults[2]