How To Returning Distance With Function In Google Maps
i've script from this link here and i want to make the function returning distance, this's my actually script : var calcRoute = function(origin,destination) { var dist; var
Solution 1:
Until the directionsService.route
function is executed the function calcRoute
has already executed and returned dist
which will be undefined.
You will get the value inside the callback function of directionsService.route
You can add another parameter (a callback function) to calcRoute
function. Now once directionsService.route
gets the response you can pass the value to this new callback function.
Try this.
var calcRoute = function(origin,destination,cb) {
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);
cb(null, response.routes[0].legs[0].distance.value / 1000);
}
else {
cb('pass error information');
}
});
};
calcRoute("-7.048357, 110.418877","-7.048443, 110.441022", function (err, dist) {
if (!err) {
$scope.resto = dist;
}
});
Post a Comment for "How To Returning Distance With Function In Google Maps"