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

   }
Mukesh Kalgude
  • 4,814
  • 2
  • 17
  • 32
Anandh Sp
  • 787
  • 6
  • 24
  • 1
    make `var test_values = [];` global and the function will have access to it when you call it. – mdamia Aug 25 '15 at 05: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? – geocodezip Aug 25 '15 at 06:00
  • @geocodezip you made a good point. I have updated my answer based on your point – user786 Aug 25 '15 at 06:09
  • Check my updated answer and a link to a similar problem. you can't return values when you do ajax call. – mdamia Aug 25 '15 at 06:45

2 Answers2

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

    });
  }

 }
user786
  • 3,902
  • 4
  • 40
  • 72
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

Community
  • 1
  • 1
mdamia
  • 4,447
  • 1
  • 24
  • 23