I tried and search many questions but not able to solve this problem. I actually want to get calculated distance value from function of a function but on first execution it is returning undefined
value, but when i execute second time it returns the proper value.
var directionsService = new google.maps.DirectionsService();
var Distance;
// Calculate Distance
function calcRoute() {
var source = document.getElementById("txtSource").value;
var destination = document.getElementById("txtDestination").value;
var distanceInput = document.getElementById("distance");
var request = {
origin: source,
destination: destination,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
Distance = response.routes[0].legs[0].distance.value / 1000;
} else {
alert("Unable to find the distance via road.");
}
});
alert("if condition value is : " + Distance);
}