我有一个功能


  calcDistance()


使用googlemaps计算并显示从地点a到地点b的路线。在calcDistance内部有service.getDistanceMatrix(这叫什么?一个函数?),其中在请求完成后有回调函数。
在该函数回调中,我想将testres返回到输入,以得到类似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>


谢谢您阅读我的问题。对不起,我给我看了这么多代码。

最佳答案

我相信service.getDistanceMatrix()必须是async代码,可能需要一些时间才能将结果取回,因此从result = calcDistance();之类的函数返回结果的传统方式将行不通。取而代之的是,您可以传递可以在service.getDistanceMatrix的回调函数中调用的回调函数,如下所示:

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);

10-08 16:36