多次调用后多个方向请求需要更多时间

时间:2014-01-06 18:15:13

标签: javascript google-maps google-maps-api-3

假设你有一张地图在其上显示KmlLayer对象,每个KmlLayer对象的切换复选框位于面板中。

var Province = new google.maps.LatLng(47.112754, -70.815223);
var map = new google.maps.Map( document.getElementById('map'), {zoom:7,center:Province} ); 

并且您在KML文件中存储有限数量的路线,以优化地图的速度。

方向的请求是使用Google Maps API v3。

让我们假设您在隐藏的div中有一个包含Google Map的页面,并通过此API发出Direction请求。 数组'waypoints'保存所需每条折线的每个起点和终点的坐标。

var geo_coords = ''; // holds the results
var y; // counter
var delay_per_request = 1000; // default delay in milliseconds between requests
var current_delay = delay_per_request; // current delay between requests

function calcRoute(start,end) {
   setTimeout(function(){
      var directionsService = new google.maps.DirectionsService();
      var request = {
         origin: start,
         destination: end,
         travelMode: google.maps.TravelMode.DRIVING
      };

      directionsService.route(request, function(response, status) {
         // Error?
         if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {

            document.getElementById('error_text').innerHTML += "<br>" + start + "/" + end
               + "("+status+") :: Interval:"+current_delay+"ms";
            window.scrollTo(0, document.body.scrollHeight);

            if (current_delay < 2600) {
               current_delay += 200;   
            }
            if (current_delay >= 1600) {
               delay_per_request = 1600;
            }

            retry++;

            if (retry < 20) {
               calcRoute(start,end);
            } else {
               document.getElementById('error_text').innerHTML += "<br>The script stopped running after "+retry+" retries"
                  + " sur " + start + "/" + end + "("+status+") :: Interval:"+current_delay+"ms";
            }

         // Positive result?
         } else if (status == google.maps.DirectionsStatus.OK) {
            var myRoute = response.routes[0].overview_path;

            for (i in myRoute) {
               if (myRoute[i] != undefined) {
                  geo_coords += myRoute[i].lat() + "," + myRoute[i].lng() + "_";
               }
            }   

            geo_coords += "!";
            document.getElementById('error_text').innerHTML += "<br><font color=\"green\">" + start + "/" + end
               + "("+status+") :: Interval:"+current_delay+"ms</font>"; 
            window.scrollTo(0, document.body.scrollHeight);

            current_delay = delay_per_request;
            retry = 0;

            Next(); 
         }
      }); 
   },current_delay);
}

function Next() {
   if (y <= waypoints.length) {
      destination = "from:" + waypoints[y] + " to:" + waypoints[y + 1];
      geo_desc += description[y] + " => " + description[y+1] + "|";
      calcRoute(waypoints[y],waypoints[y+1]);
      y = y +2;
   } else {
      document.getElementById("information").setAttribute('value',geo_coords);   
      document.getElementById("show_debug_text").setAttribute('value',geo_desc); 
      document.getElementById("error").setAttribute('value',document.getElementById('error_text').innerHTML);
      document.forms["kml"].submit();
   }
}  

Next();

我的问题如下:
是否有人注意到Google在您批量提出请求后降低了请求的优先级?
您执行的请求越多,您获得的OVER_QUERY_LIMIT响应的数量就越大,成功结果之前的请求之间的延迟就越大,成功结果之前请求的数量就越大。

有任何解决方法或任何原因吗?
(我知道我应该使用Direction API Webservice来提出我的请求。)
谢谢!

0 个答案:

没有答案