0
<script type=text/javascript src=https://maps.googleapis.com/maps/api/js?    sensor=false></script>
<script language=JavaScript>
var mileageidx = new Number();
function getMileage(idx)
{
  // code snipped
  mileageidx = idx;
  var service = new google.maps.DistanceMatrixService();
  service.getDistanceMatrix({ origins: [zipCode], destinations: [hostClubZipCode],    travelMode: google.maps.TravelMode.DRIVING, unitSystem: google.maps.UnitSystem.IMPERIAL, avoidHighways: false, avoidTolls: false}, callback());
  // code snipped
}
function callback(response, status)
{
  alert('in callback routine, mileageidx ='+mileageidx+' google status = '+status);
  if (status == google.maps.DistanceMatrixStatus.OK)
  {
    var origins = response.originAddresses;
    var destinations = response.destinationAddresses;
    for (var i = 0; i < origins.length; i++)
    {
      var results = response.rows[i].elements;\n";
      for (var j = 0; j < results.length; j++)\n";
      {
        var element = results[j];
        var distance = element.distance.text;
        alert('distance='+distance);
        var duration = element.duration.text;
        var from = origins[i];
        var to = destinations[j];
      }
    }
  }
  else
  {
    if (status == google.maps.DistanceMatrixStatus.INVALID_REQUEST)
    {
      alert('google.maps.DistanceMatrixStatus was INVALID_REQUEST');
    }
    if (status == google.maps.DistanceMatrixStatus.MAX_ELEMENTS_EXCEEDED)
    {
      alert('google.maps.DistanceMatrixStatus was MAX_ELEMENTS_EXCEEDED');
    }
    if (status == google.maps.DistanceMatrixStatus.OVER_QUERY_LIMIT)
    {
      alert('google.maps.DistanceMatrixStatus was OVER_QUERY_LIMIT ');
    }
    if (status == google.maps.DistanceMatrixStatus.REQUEST_DENIED)
    {
      alert('google.maps.DistanceMatrixStatus was REQUEST_DENIED');
    }
    if (status == google.maps.DistanceMatrixStatus.UNKNOWN_ERROR)
    {
      alert('google.maps.DistanceMatrixStatus was UNKNOWN_ERROR');
    }
  }
}

执行代码时,我收到消息“b”为空或不是对象 DistanceMatrixService.getDistanceMatrix 第 1 行代码:0 字符:1 URL:https://maps.googleapis.com/maps/api/js/DistanceMatrixService。 GetDistanceMatrix?1m1&2s80 ...

此代码工作了两次,然后开始出现此错误。

4

1 回答 1

0

发现问题了,如果在同一个范围内(例如同一个例程),似乎代码的两个部分是冲突的。

当我将 api 移到它自己的例程中时,一切都开始工作了。冲突/导致错误的代码是:

xmlhttp.open(\"GET\",getClubURL,false);
xmlhttp.send();
textToParse = xmlhttp.responseText;

function calculateDistances(start,end)
{
  //alert('Made it to calculateDistances');
  var service = new google.maps.DistanceMatrixService();
  alert('In calculateDistances, start is '+start+', end is '+end);
  service.getDistanceMatrix(
  {
    origins: [start],
    destinations: [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;
    document.getElementById("mileage[mileageidx]").value =
                                                   response.destinationAddresses;
    for (var i = 0; i < origins.length; i++)
    {
      var results = response.rows[i].elements;
      for (var j = 0; j < results.length; j++)
      {
         var tmpTxt = new String(results[j].distance.text);
         eos = tmpTxt.indexOf(' ');
         alert('eos = '+eos);
         tmpTxt = tmpTxt.substr(0,eos);
         alert('tmpTxt is '+tmpTxt);
         var miles = new Number(tmpTxt);
         miles = Math.round(miles);
         document.getElementById(\"mileage[\"+mileageidx+\"]\").value=miles;
      }
    }
  }
} // End of callback routine
于 2012-05-24T02:00:27.963 回答