本文介绍了谷歌地图JavaScript API中的方向服务路线上的SetTimeout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我使用Google Map JavaScript API的Directionservice。其实我无法在我的地图上显示超过10条路线。如果我试图显示超过10条路线,那么它会抛出OVER_QUERY_LIMIT错误。 即使在设置setTimeout函数后无法显示所有路线。我只使用20条路线在地图上显示,无法显示这20条路线。但是,如果我使用setInterval函数,那么我可以看到所有的路由,但无法使用clearInterval函数来停止时间间隔,因此它将进入无限循环。I am using Directionservice of Google Map JavaScript API. Actually I am not able to display more than 10 routes in my map. If I am trying to display more than 10 routes then it is throwing the OVER_QUERY_LIMIT error.Even though after setting the setTimeout function not able to display all the routes. I am using only 20 routes to display on the map and not able to display those 20 routes. But if I use the setInterval function then am able to see the all the routes but not able to stop the interval with clearInterval function too so it is going in infinite loop.function buildRoute(from, to, fromId, toId, index) { setTimeout(function() { var directionsService = new google.maps.DirectionsService(); var color = setColor(index); var rendererOptions = { map: myMap, suppressMarkers: true, polylineOptions: { strokeColor: color } }; var directionsRequest = { origin: from, destination: to, travelMode: getTraveMode(), unitSystem: google.maps.UnitSystem.METRIC }; directionsService.route(directionsRequest, function(response, status) { count = count + 1; console.log(count + " " + fromId + " " + toId); if (status != google.maps.DirectionsStatus.OK) console.log(status); if (status == google.maps.DirectionsStatus.OK) { counter = counter + 1; var directionsDisplay = new google.maps.DirectionsRenderer( rendererOptions); directionsDisplay.setDirections(response); custimiseDriection(response, fromId, toId, color); if (counter == totRoutes) { // clearInterval(); displayTimeandDistance(); } } }) }, 1000)} SetTimeout函数是在以下代码的不同位置使用,如下所示,但无法克服OVER_QUERY_LIMIT。The SetTimeout function is used at different places in the below code as like below but not able to overcome the OVER_QUERY_LIMIT. setTimeOut(buildRoute(from,to,fromId ,toId,index),1000) 在setTimeout函数中包含directionservice.route函数 只包含directionservice.route的回调函数代码是setTimeout函数。以下是Chrome控制台中OVER_QUERY_LIMIT错误的屏幕截图。 OVER QUERY LIMIT错误Here is the screen shot of OVER_QUERY_LIMIT errors in chrome console.OVER QUERY LIMIT Error推荐答案客户端方向服务是为了响应用户交互而创建的,这就是为什么你的请求被限制的原因 - 你不能指望JS Maps API库能够执行多于10个请求。The client side DirectionService is created to respond to user interaction and this is why your requests are getting throttled - you cannot expect the JS Maps API library to be able to perform more than 10 requests in a burst.如果您需要分析多个路由,您可以考虑使用服务器端定向服务,因为它的使用限制有文档记录,您可以启用结算以获得更高的配额。If you need to make an analysis of a number of routes you can consider using the server side directions service since it has usage limits which are documented and you can enable billing to get higher quotas. 这篇关于谷歌地图JavaScript API中的方向服务路线上的SetTimeout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-20 01:20