ViniBiso
ViniBiso

Reputation: 179

Google Maps API v3 How can this callback function work?

So here is my problem. The callback function is not called. And I'dont know why. Or even how this works. How can I call a function with parameters and not put the parameters on the call?

Here's the code

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

Upvotes: 0

Views: 798

Answers (1)

geocodezip
geocodezip

Reputation: 161334

You are missing the 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 | Type of routing requested. Required

Upvotes: 1

Related Questions