返回错误以未定义的形式返回

时间:2014-01-20 07:04:56

标签: jquery

我正在调用 google distancematrix api 并测试我传递给函数的值作为正确的lat lon值。

我在调用脚本时得到results[j].distance undefined 当它到达

调用距离calc的基本脚本

  //get estimated distance from coords
  var site1= $('#s1id').val(), site2=$('#s2id').val();
  if(site1 != ' ' && site2 != ' '){
    initialise_geocode();
    set_origin(site1_geo)
    set_destination(site2_geo);
    distance = calcDistance();
    alert(distance);
  }

这是我正在调用的脚本

// add to top of page for distance matrix calculations
//<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>

//setup usage variables
var geocoder= '';
var origin= '';
var destination ='';
var distance = '';

function initialise_geocode(){
    geocoder = new google.maps.Geocoder();
}

function set_origin(location = null){
    origin = new google.maps.LatLng(location);
}

function set_destination(location = null){
    destination = new google.maps.LatLng(location);

}

function calcDistance() {
  var service = new google.maps.DistanceMatrixService();
  service.getDistanceMatrix(
    {
      origins: [origin],
      destinations: [destination],
      travelMode: google.maps.TravelMode.DRIVING,
      unitSystem: google.maps.UnitSystem.METRIC,
      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;


    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);
       // outputDiv.innerHTML 
        alert('  '+ origins[i] + ' to ' + destinations[j]
            + ': ' + results[j].distance.text + ' in '
            + results[j].duration.text + '<br>');
      }
    }
  }
}

0 个答案:

没有答案