-1

Here I am calculating the distance and time between two latitude and longitude points.Almost I am getting the answer but I am not able to return the value to the function.

Please help me. Thanks in advance

My codings are :

function initMap() {
         console.log(getDistanceandTime(srcRouteAddress.lat,srcRouteAddress.lng,destRouteAddress.lat,destRouteAddress.lng));

        function getDistanceandTime(lat1,lon1,lat2,lon2){
        var origin = {lat: parseFloat(lat1), lng: parseFloat(lon1)};
        var destination = {lat: parseFloat(lat2), lng: parseFloat(lon2)};
        var service = new google.maps.DistanceMatrixService;
        //var test = [];
        service.getDistanceMatrix({
            origins: [origin],
            destinations: [destination],
            travelMode: google.maps.TravelMode.DRIVING
        }, function (response, status) {
            if (status == google.maps.DistanceMatrixStatus.OK) {
                var test_values = [];
                var originList = response.originAddresses;
                for (var i = 0; i < originList.length; i++) {
                    var results = response.rows[i].elements;
                    for (var j = 0; j < results.length; j++) {
                        var test = results[j].distance.text + ' in ' + results[j].duration.text;
                        test_values.push(test);
                        console.log(test_values);
                    }
                }
                return test_values;
            }
            return test_values;
        });
    }

   }
4
  • 1
    make var test_values = []; global and the function will have access to it when you call it. Commented Aug 25, 2015 at 5:51
  • 2
    The Distance Matrix is asynchronous, you have to use the data inside the callback when/where it is available. What are you trying to do with the test_values array? Commented Aug 25, 2015 at 6:00
  • @geocodezip you made a good point. I have updated my answer based on your point Commented Aug 25, 2015 at 6:09
  • Check my updated answer and a link to a similar problem. you can't return values when you do ajax call. Commented Aug 25, 2015 at 6:45

2 Answers 2

0

if it is a synchronous call, then try this, your var test_values = []; should be outside if (status == google.maps.DistanceMatrixStatus.OK) condition. And most probably insideif (status == google.maps.DistanceMatrixStatus.OK) is not executing

also google.maps.DistanceMatrixStatus.OK is asynchronous so better to return value inside if(google.maps.DistanceMatrixStatus.OK)

So try this

   function initMap() {
     console.log(getDistanceandTime(srcRouteAddress.lat,srcRouteAddress.lng,destRouteAddress.lat,destRouteAddress.lng));

    function getDistanceandTime(lat1,lon1,lat2,lon2){
    var origin = {lat: parseFloat(lat1), lng: parseFloat(lon1)};
    var destination = {lat: parseFloat(lat2), lng: parseFloat(lon2)};
    var service = new google.maps.DistanceMatrixService;
    //var test = [];
    service.getDistanceMatrix({
        origins: [origin],
        destinations: [destination],
        travelMode: google.maps.TravelMode.DRIVING
    }, function (response, status) {
                         var test_values = [];
        if (status == google.maps.DistanceMatrixStatus.OK) {

            var originList = response.originAddresses;
            for (var i = 0; i < originList.length; i++) {
                var results = response.rows[i].elements;
                for (var j = 0; j < results.length; j++) {
                    var test = results[j].distance.text + ' in ' + results[j].duration.text;
                    test_values.push(test);
                    console.log(test_values);
                }
            }
            return test_values;
        }

    });
  }

 }
Sign up to request clarification or add additional context in comments.

4 Comments

Again when i call the function in console it showing undefined
check what u are getting in results.length
and in originList.lengt
0

Try this..

var test_values = []; // global variable
function initMap() {
    // your code here
}

function showLatLng(){
    console.log(test_values); // will output the content
}

Javascript runs on the UI thread; if your code waits for the server to reply, the browser must remain frozen. Ajax jquery async return value

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.