Google Maps API v3此回调函数如何工作?

时间:2013-07-19 23:51:11

标签: javascript google-maps-api-3

所以这是我的问题。 不调用回调函数。我不知道为什么。甚至这是如何工作的。 如何调用带参数的函数而不将参数放在调用上?

这是代码

    var addr = new Array(5);
    addr[0] = new google.maps.LatLng(-27.352646,-53.384881);
    addr[1] = new google.maps.LatLng(-27.344648,-53.395009);
    addr[2] = new google.maps.LatLng(-27.365562,-53.388859);
    addr[3] = new google.maps.LatLng(-27.366241,-53.401655);
    addr[4] = new google.maps.LatLng(-27.360467,-53.397476);


    var a = new google.maps.LatLng(-27.352901,-53.402745);
    var menorDistancia;
    var destinoFinal;
    function callback(response, status) {
        alert("CHEGOU AQUI")
      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;
      for (var j = 0; j < results.length; j++) {
        var element = results[j];
        var distance = element.distance.text;
        var duration = element.duration.text;
        var from = origins[i];
        var to = destinations[j];
        if(distance < menorDistancia || i==0){
            menorDistancia = distance;
            destinoFinal = to;
        }
      }
    }
  }
    }

    var service = new google.maps.DistanceMatrixService();
    service.getDistanceMatrix(
        {
            origins: [a,a,a,a,a],
            destinations: [addr[0],addr[1],addr[2],addr[3],addr[4]],
        }, callback);

1 个答案:

答案 0 :(得分:1)

你错过了travelMode:

service.getDistanceMatrix(
    {
        origins: [a,a,a,a,a],
        destinations: [addr[0],addr[1],addr[2],addr[3],addr[4]],
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    }, callback);

per the documentation

  

travelMode | TravelMode |请求的路由类型。的必需