1

i have this function and i need to populate result variable with response. I googled it for a few hours but unfortunately. Question ... is it possible?

function getRoute() {
   var result; // array or object

   directionsService.route(request, function(response, status) {
      if (status == google.maps.DirectionsStatus.OK) {
         directionsRenderer.setDirections(response);
         console.log(response.routes[0].legs[0].distance.text);
         console.log(response.routes[0].legs[0].duration.text);
      }
   });

   return result;
}

result returned as array or object

var result = getRoute();
    console.log(result);  // anything like {'direction': 0, 'distance': 0}

thanks for advice or ideas

daremachine
  • 2,678
  • 2
  • 23
  • 34

1 Answers1

0

The directions service is asynchronous, it can't return the data. If you need to use the result, use it in the callback function. If you need it available later, you can assign it to a global variable (or store it in a hidden input field).

geocodezip
  • 158,664
  • 13
  • 220
  • 245
  • hi, thank you for your response. I use [link](http://nlaplante.github.io/angular-google-maps/#!/usage) and implement direction service into directive. I need to return reponse to controller. I find this post [link](http://stackoverflow.com/questions/13318726/easiest-way-to-pass-an-angularjs-scope-variable-from-directive-to-controller). You think is possible this? – daremachine Jun 28 '13 at 19:43