I have a script obtained from this link here and I am trying to create a function that returns the distance. This is my current script:
var calcRoute = function(origin, destination) {
var dist;
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
directionsDisplay = new google.maps.DirectionsRenderer();
var request = {
origin:origin,
destination:destination,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
dist = response.routes[0].legs[0].distance.value / 1000;
}
});
return dist;
};
$scope.resto = calcRoute("-7.048357, 110.418877","-7.048443, 110.441022");
I have included two parameters in the function, and I expect it to return the distance. However, the script does not seem to be returning the value from
dist = response.routes[0].legs[0].distance.value / 1000
.
I am using angularjs and the resto in my View is not displaying the distance. Can anyone help me? Is there something wrong with the script or is it something else?