0

我试图计算距离 2 个非常远的位置的距离,我希望谷歌给我一个零结果错误,因为没有从这 2 个位置找到正确的方法,但没有给我这个错误而且我无法拦截这个错误只是说我:

未捕获的类型错误:无法读取未定义的属性“文本”

现在我的问题是,我怎样才能拦截这个错误零结果?

var map;

function initMap() {
    map = new google.maps.Map(document.getElementById('map'), {
        zoom: 5,
        center: {
            lat: -15.7942357,
            lng: -47.8821945
        }
    });
    
    var bounds = new google.maps.LatLngBounds;

        var origin1 = {lat: -33.8688197, lng: 151.209295500000058};
        var destinationB = {lat: 50.087, lng: 14.421};


        var geocoder = new google.maps.Geocoder;

        var service = new google.maps.DistanceMatrixService;
        service.getDistanceMatrix({
          origins: [origin1],
          destinations: [destinationB],
          travelMode: 'DRIVING',
          unitSystem: google.maps.UnitSystem.METRIC,
          avoidHighways: false,
          avoidTolls: false
        }, function(response, status) {
            
          if (status !== 'OK') {
            alert('Error was: ' + status);
          } else {
            var originList = response.originAddresses;
            var destinationList = response.destinationAddresses;

            var outputDiv = document.getElementById('output');



            var showGeocodedAddressOnMap = function(asDestination) {
              return function(results, status) {
         
                  
                if (status === 'OK') {
                  map.fitBounds(bounds.extend(results[0].geometry.location));

                } else {
                  alert('Geocode was not successful due to: ' + status);
                }
              };
            };

            for (var i = 0; i < originList.length; i++) {
              var results = response.rows[i].elements;
      
                     
              geocoder.geocode({'address': originList[i]},
                  showGeocodedAddressOnMap(false));
              for (var j = 0; j < results.length; j++) {
                geocoder.geocode({'address': destinationList[j]},
                    showGeocodedAddressOnMap(true));

                    alert(results[j].distance.text);
                    alert(results[j].duration.text);  

              }
            }
          }
        });

    
    
      }
#map{
width:100%;
height:300px;
}
<html>
<head>
</head>
<body>
<div id="map"></div>
<script async defer
    src="https://maps.googleapis.com/maps/api/js?key=AIzaSpQw&libraries=places&callback=initMap">
    </script>

</body>
</html>

4

2 回答 2

1

正如我所提到的,请求没问题,因此它返回状态,好吧,您需要检查每个元素的状态。查看下面的代码,尤其是该行results[j].status !== "OK"

var map;

function initMap() {
  map = new google.maps.Map(document.getElementById('map'), {
    zoom: 5,
    center: {
      lat: -15.7942357,
      lng: -47.8821945
    }
  });

  var bounds = new google.maps.LatLngBounds;

  var origin1 = {
    lat: -33.8688197,
    lng: 151.209295500000058
  };
  var destinationB = {
    lat: 50.087,
    lng: 14.421
  };


  var geocoder = new google.maps.Geocoder;

  var service = new google.maps.DistanceMatrixService;
  service.getDistanceMatrix({
    origins: [origin1],
    destinations: [destinationB],
    travelMode: 'DRIVING',
    unitSystem: google.maps.UnitSystem.METRIC,
    avoidHighways: false,
    avoidTolls: false
  }, function(response, status) {

    if (status !== 'OK') {
      alert('Error was: ' + status);
    } else {
      var originList = response.originAddresses;
      var destinationList = response.destinationAddresses;

      var outputDiv = document.getElementById('output');



      var showGeocodedAddressOnMap = function(asDestination) {
        return function(results, status) {


          if (status === 'OK') {
            map.fitBounds(bounds.extend(results[0].geometry.location));

          } else {
            alert('Geocode was not successful due to: ' + status);
          }
        };
      };

      for (var i = 0; i < originList.length; i++) {
        var results = response.rows[i].elements;


        geocoder.geocode({
            'address': originList[i]
          },
          showGeocodedAddressOnMap(false));
        for (var j = 0; j < results.length; j++) {
          geocoder.geocode({
              'address': destinationList[j]
            },
            showGeocodedAddressOnMap(true));

          if (results[j].status !== "OK") {
            alert("Not okay");
            return;
          }

          alert(results[j].distance.text);
          alert(results[j].duration.text);

        }
      }
    }
  });



}
#map {
  width: 100%;
  height: 300px;
}
<html>

<head>
</head>

<body>
  <div id="map"></div>
  <script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCp5RzbQjgID4oHJYe6VRGhKGXpQTGtCmw&libraries=places&callback=initMap">
  </script>

</body>

</html>

于 2017-05-05T14:01:58.153 回答
1

在发出警报之前,请检查以下内容

// check if it has results
if(results[j].status === "OK") {
    // do something with result
    alert(results[j].distance.text);
    alert(results[j].duration.text);
}

谷歌地图 API 状态码

于 2017-05-05T14:02:54.623 回答