JavaScript - Google Maps: Total Distance function returning undefined -


i'm trying use directionsservice find total distance of route waypoints, whenever try use function, keeps returning undefined, although displays correct answer. here's code:

function addhexwaypts()    firstpoint_1 = new google.maps.geometry.spherical.computeoffset(tempwaypt, distanceside*1000, (directions[0]+90));   secondpoint_1 = new google.maps.geometry.spherical.computeoffset(firstfirstpoint, onethird*1000, directions [0]);    firstpoint_2 = new google.maps.geometry.spherical.computeoffset(tempwaypt, distanceside*1000, (directions[0]-90));   secondpoint_2 = new google.maps.geometry.spherical.computeoffset(secondfirstpoint, onethird*1000, directions [0]);    var firstroute = checkthishex(firstpoint_1, secondpoint_1,0);   var secondroute = checkthishex(firstpoint_2, secondpoint_2,1);    //checkthishex function in question    var bool = comparehex(firstroute, secondroute);    if (bool = true)   {     fillwaypts(firstfirstpoint, firstsecondpoint);   }   else   {     fillwaypts(secondfirstpoint, secondsecondpoint);   }   calculateanddisplayroute(); }  function checkthishex(first, second, num) {   var total = 0;   var number = 0;   var waypointstesting = [];   waypointstesting.push({     location: first,     stopover: false   });   waypointstesting.push({     location: second,     stopover: false   });   directionsservice.route({     origin: document.getelementbyid('routestart').value,     destination: document.getelementbyid('routeend').value,     waypoints: waypointstesting,     optimizewaypoints: true,     travelmode: 'bicycling'   },   function(response, status) {     if (status === 'ok')     {        var myroute = response.routes[0];       (var = 0; < myroute.legs.length; i++) {         total += myroute.legs[i].distance.value;       }       total = total / 1000;        if (num==0)       {         document.getelementbyid("printinfo1").innerhtml = total;         return total;       }       else       {         document.getelementbyid("printinfo2").innerhtml = total;         return total;       }       //i use "num" display distances of both routes, works... keeps returning undefined     }   }); } 

the routing service doesn't return sychronously so

  var firstroute = checkthishex(firstpoint_1, secondpoint_1,0);   var secondroute = checkthishex(firstpoint_2, secondpoint_2,1); 

will undefined. need pass callback function checkthishex so:

function checkthishex(first, second, num,callback) {    ....    if (num==0)       {         document.getelementbyid("printinfo1").innerhtml = total;         callback(total);       }       else       {         document.getelementbyid("printinfo2").innerhtml = total;         callback(total);       }  } 

and change first function in lines of:

  checkthishex(firstpoint_1, secondpoint_1,0,function(firstroute) {        checkthishex(firstpoint_2, secondpoint_2,1,function(secondroute){            var bool = comparehex(firstroute, secondroute);          if (bool = true)         {           fillwaypts(firstfirstpoint, firstsecondpoint);         }         else         {           fillwaypts(secondfirstpoint, secondsecondpoint);         }         calculateanddisplayroute();        });    }); 

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -