javascript - Google API and wrong result from its own function directionService -


i' m having problem , when load page , insert origin , destination, after clicking button "locate" doesn't show in google map, because says response not object , tried stamp console.log , says response=null , if reload page , click fast on locate , draws route.

here's code

function init(){     var latlng = new google.maps.latlng(40.635636, 17.942414);     var mapoptions = { zoom: 12, center: latlng  };     map = new google.maps.map(document.getelementbyid('map-canvas'), mapoptions); }  function updatemap(){     init();      var origingeocoder = new google.maps.geocoder();     var destinationgeocoder = new google.maps.geocoder();      var origin = document.getelementbyid( "origin" ).value + " brindisi 72100";     var destination = document.getelementbyid( "destination" ).value + " brindisi 72100";       var directionsservice2 = new google.maps.directionsservice();      origingeocoder.geocode( { 'address': origin }, function(results, status) {         if ( status == google.maps.geocoderstatus.ok ) {             var startlatlng = results[0].geometry.location;             var olat = startlatlng.lat();             var olng = startlatlng.lng();             document.getelementbyid('cstart').innerhtml = olat + " " + olng;         }         else{             alert("geocode not successful following reason: " + status);         }           });      //chiamata asincrona alle api per ottenere lat e lng dell' indirizzo di destinazione     destinationgeocoder.geocode( { 'address': destination }, function(results, status) {         if ( status == google.maps.geocoderstatus.ok ) {             var destlatlng = results[0].geometry.location;             var dlat = destlatlng.lat();             var dlng = destlatlng.lng();             document.getelementbyid('cdestination').innerhtml = typeof dlat;             document.getelementbyid('cdestination').innerhtml = dlat + " " + dlng;         }         else{             alert("geocode not successful following reason: " + status);         }             });      //salva in req[] le varie distanze tra le paline e la destinazione     singleobjtostop(origin,destination,function(paline,req,reqo){         console.log("1");         //trova la palina piĆ¹ vicina alla destinazione         calcsingledis(paline,req,reqo,function(w2,w1){             console.log("2");             //disegna waypoints(?)             reqend(origin,destination,w1,w2,function(request){                 console.log("3");                 directionsservice2.route(request, function(response, status) {                     console.log("4");                     console.log(response);                     if (status == google.maps.directionsstatus.ok) {                         directionsdisplay.setdirections(response);                         var route = response.routes[0];                         var summarypanel = document.getelementbyid("distance");                         summarypanel.innerhtml = "";                         // each route, display summary information.                         (var = 0; < route.legs.length; i++) {                             var routesegment = + 1;                             summarypanel.innerhtml += "<b>route segment: " + routesegment + "</b><br />";                             summarypanel.innerhtml += route.legs[i].start_address + " ";                             summarypanel.innerhtml += route.legs[i].end_address + "<br />";                             summarypanel.innerhtml += route.legs[i].distance.text + " ";                             summarypanel.innerhtml += route.legs[i].duration.text +  "<br /><br />" ;                         }                         computetotaldistance(response);                     }                     else{                         console.log("entra qua stronzo");                         console.log("fermata partenza = " + w1);                         console.log("fermata arrivo = " + w2);                     }                 });                 directionsdisplay.setmap(map);             });         });     }); }  function singleobjtostop(origin,destination,callback){     var data=<?php echo $data; ?>;     var a,b,i=0;     var paline = new array();     var req = new array();      var reqo = new array();     var num = <?php echo $n; ?>;     $.each(data, function(fieldname, fieldvalue) {         a=fieldvalue.geolat;         b=fieldvalue.geolong;         a=parsefloat(a);         b=parsefloat(b);         paline[i]=new google.maps.latlng(a,b);          req[i] = {             origin:paline[i],             destination:destination,             travelmode: google.maps.travelmode.walking         };          reqo[i] = {             origin:origin,             destination:paline[i],             travelmode: google.maps.travelmode.walking         };          i++;         if(i==num){             callback(paline,req,reqo);         }     }); }  function calcsingledis(paline, req, reqo, callback) {     var directionsservice = new google.maps.directionsservice();     var c = 10000000;     var w2 = new google.maps.latlng(0, 0);     var w1 = new google.maps.latlng(0, 0);     var num = <?php echo $n; ?>;     var j = (num - 1);     var t;      var co = 10000000;     var numo = <?php echo $n; ?>;     var jo = 0;     (j = 0; j < num; j++) {         t = 0;         directionsservice.route(req[j], function(response, status) {             if (status == google.maps.directionsstatus.ok) {                 //directionsdisplay.setdirections(response);                 var troute = response.routes[0];                 var dis = parsefloat((troute.legs[0].distance.text).replace(",", "."));                  document.getelementbyid('test').innerhtml = dis;                  //se distanza minore di quella minore trovata fin ora la cambia                 if (dis < c) {                     w2 = paline[j - num];                     c = dis;                 }                  if (t == (num - 1)) {                     console.log("qua entra lol");                      (jo = 0; jo < numo; jo++) {                         console.log("e non entra mannac");                         t = 0;                         directionsservice.route(reqo[jo], function(response, status) {                             if (status == google.maps.directionsstatus.ok) {                                 console.log("e non entra mannac22222");                                 //directionsdisplay.setdirections(response);                                 var troute = response.routes[0];                                 var diso = parsefloat((troute.legs[0].distance.text).replace(",", "."));                                  document.getelementbyid('test').innerhtml = dis;                                  //se distanza minore di quella minore trovata fin ora la cambia                                 if (diso < co) {                                     w1 = paline[jo - numo];                                     co = diso;                                 }                                  if (t == (numo - 1)) {                                     console.log("w1 = " + w1);                                     console.log(response);                                     callback(w2, w1);                                  }                             }                             jo++;                             t++;                         });                     }                  }             }             j++;             t++;         });     } }  function reqend(origin,destination,w1,w2,callback){     var request = {         origin:origin,         destination:destination,           waypoints: [{location: w1} , {location: w2}],           //waypoints: [{location: w2}],           optimizewaypoints: true,         travelmode: google.maps.directionstravelmode.walking     };     callback(request); }  function computetotaldistance(result) {     var totaldist = 0;     var totaltime = 0;     var myroute = result.routes[0];     (i = 0; < myroute.legs.length; i++) {         totaldist += myroute.legs[i].distance.value;         totaltime += myroute.legs[i].duration.value;        }     totaldist = totaldist / 1000.     document.getelementbyid("total").innerhtml = "total distance is: "+ totaldist + " km<br>total time is: " + (totaltime / 60).tofixed(2) + " minutes"; }  google.maps.event.adddomlistener( window, 'load', init ); 

the problem related limit of query can use google maps api v3. can take here: https://developers.google.com/maps/documentation/business/faq#google_maps_api_services

you lots of requests api program, while have quite restrictive limits can see google q&a.

applications should throttle requests avoid exceeding usage limits, bearing in mind these apply each client id regardless of how many ip addresses requests sent from.

you can throttle requests putting them through queue keeps track of when requests sent. rate limit or 10 qps (queries per second), on sending 11th request application should check timestamp of first request , wait until 1 second has passed. same should applied daily limits.

even if throttling implemented correctly applications should watch out responses status code over_query_limit. if such response received, use pause-and-retry mechanism explained in usage limits exceeded section above detect limit has been exceeded.

you find useful: how geocode 20 addresses without receiving over_query_limit response?


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -