2017-05-05 158 views
-1

我已嘗試計算距離2個很遠的地點的距離 ,我期望Google給我一個ZERO RESULT錯誤,因爲沒有從這兩個位置找到正確的方式,但不會給我這個錯誤,我無法攔截這個錯誤 只是說我:谷歌地圖API如何得到結果ZERO RESULT

Uncaught TypeError: Cannot read property 'text' of undefined

現在的問題是,我怎麼能截獲這個錯誤結果爲零?

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>

+1

_OK - 請求有效。即使在任何來源和目的地之間沒有找到路線,也可以返回此狀態。請參閱元素狀態代碼以瞭解元素級別的狀態信息._請閱讀[此處](https://developers.google.com/maps/documentation/javascript/distancematrix#distance_matrix_status_codes),因爲您期待得到結果列表你回來的狀態是關於一般的請求。所以你需要檢查'results [j] .status'.PS你得到這個錯誤的原因是因爲這行'alert(results [j] .distance.text);' – George

+0

Thx for answer i have read這個 ,但問題是我已提醒(狀態)這2條件「if(status ==='OK'){」 ,他們總是返回OK,而不是零結果 – Popolitus

+0

是的,你已經爲請求而不是每個元素。 – George

回答

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>

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); 
} 

Google Maps API Status Code