Vlanzyvinz Vlanzyvinz - 1 month ago 13
Javascript Question

How to get value from service.getDistanceMatrix({..........})

I have a function


calcDistance()


that calculate and show the route from place a to place b using googlemaps.
Inside calcDistance there are service.getDistanceMatrix (what this called? a function?) Inside it there are callback function when the request is complete.

Inside that function callback I want to return testres to input to result like
result = calcDistance();



function calcDistance(origin1,destinationB){
var geocoder = new google.maps.Geocoder();
var service = new google.maps.DistanceMatrixService();
var temp_duration = 0;
var temp_distance = 0;
var testres;
service.getDistanceMatrix(
{
origins: [origin1],
destinations: [destinationB],
travelMode: google.maps.TravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.METRIC,
avoidHighways: false,
avoidTolls: false
}, function(response, status) {
if (status !== google.maps.DistanceMatrixStatus.OK) {
alert('Error was: ' + status);
testres= {"duration":0,"distance":0};
} else {
var originList = response.originAddresses;
var destinationList = response.destinationAddresses;
var showGeocodedAddressOnMap = function (asDestination) {
testres= function (results, status) {
};
};
var duration_total;
for (var i = 0; i < originList.length; i++) {
var results = response.rows[i].elements;
geocoder.geocode({'address': originList[i]},
showGeocodedAddressOnMap(false));
for (var j = 0; j < results.length; j++) {
geocoder.geocode({'address': destinationList[j]},
showGeocodedAddressOnMap(true));
temp_duration+=results[j].duration.value;
temp_distance+=results[j].distance.value;
}
}
testres= {"duration":temp_duration,"distance":temp_distance};
}
}
);
}

result = calcDistance(source,dest);
</script>


Thankyou for reading my question. I'm sorry I give so many code to read.

Answer

I believe this service.getDistanceMatrix() must be an async code which might take some time to get the result back so traditional way of returning result from function like result = calcDistance(); won't work. Instead of that you can pass callback function which can be invoked in callback function of service.getDistanceMatrix as follows:

function calcDistance(origin1,destinationB,ref_Callback_calcDistance){
        var geocoder = new google.maps.Geocoder();
        var service = new google.maps.DistanceMatrixService();
        var temp_duration = 0;
        var temp_distance = 0;
        var testres;
        service.getDistanceMatrix(
            {
                origins: [origin1],
                destinations: [destinationB],
                travelMode: google.maps.TravelMode.DRIVING,
                unitSystem: google.maps.UnitSystem.METRIC,
                avoidHighways: false,
                avoidTolls: false
            }, function(response, status) {
                if (status !== google.maps.DistanceMatrixStatus.OK) {
                    alert('Error was: ' + status);
                    testres= {"duration":0,"distance":0};
                } else {
                    var originList = response.originAddresses;
                    var destinationList = response.destinationAddresses;
                    var showGeocodedAddressOnMap = function (asDestination) {
                        testres= function (results, status) {
                        };
                    };
                    var duration_total;
                    for (var i = 0; i < originList.length; i++) {
                        var results = response.rows[i].elements;
                        geocoder.geocode({'address': originList[i]},
                            showGeocodedAddressOnMap(false));
                        for (var j = 0; j < results.length; j++) {
                            geocoder.geocode({'address': destinationList[j]},
                                showGeocodedAddressOnMap(true));
                            temp_duration+=results[j].duration.value;
                            temp_distance+=results[j].distance.value;
                        }
                    }
                    testres= {"duration":temp_duration,"distance":temp_distance};

                    if(typeof ref_Callback_calcDistance === 'function'){
                        //calling the callback function
                        ref_Callback_calcDistance(testres)
                    }
                }
            }
        );
    }

function Callback_calcDistance(testres) {
    //do something with testres
}


    //calling the calcDistance function and passing callback function reference
    calcDistance(source,dest,Callback_calcDistance);
Comments