异步并等待

时间:2019-08-15 14:52:02

标签: google-maps asynchronous google-maps-api-3 async-await

我想为zipCode绘制标记。但是我只能看到一些标记。 我以为是因为异步和等待,但是我不知道在哪里添加它们。 有人请帮助我。

══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════
The following TestFailure object was thrown running a test:
  Expected: exactly 3 matching nodes in the widget tree
  Actual: ?:<zero widgets with type "ListTile" (ignoring offstage widgets)>
   Which: means none were found but some were expected

When the exception was thrown, this was the stack:
#4      main.<anonymous closure> (file:///path/to/project/search_delegate_test/test/serach_delegate_problem_test.dart:37:5)
<asynchronous suspension>
#5      testWidgets.<anonymous closure>.<anonymous closure> (package:flutter_test/src/widget_tester.dart:118:25)
<asynchronous suspension>
#6      TestWidgetsFlutterBinding._runTestBody (package:flutter_test/src/binding.dart:630:19)
<asynchronous suspension>
#9      TestWidgetsFlutterBinding._runTest (package:flutter_test/src/binding.dart:613:14)
#10     AutomatedTestWidgetsFlutterBinding.runTest.<anonymous closure> (package:flutter_test/src/binding.dart:1010:24)
#16     AutomatedTestWidgetsFlutterBinding.runTest (package:flutter_test/src/binding.dart:1007:15)
#17     testWidgets.<anonymous closure> (package:flutter_test/src/widget_tester.dart:116:22)
#18     Declarer.test.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:test_api/src/backend/declarer.dart:168:27)
<asynchronous suspension>
#19     Invoker.waitForOutstandingCallbacks.<anonymous closure> (package:test_api/src/backend/invoker.dart:250:15)
<asynchronous suspension>
#24     Invoker.waitForOutstandingCallbacks (package:test_api/src/backend/invoker.dart:247:5)
#25     Declarer.test.<anonymous closure>.<anonymous closure> (package:test_api/src/backend/declarer.dart:166:33)
#30     Declarer.test.<anonymous closure> (package:test_api/src/backend/declarer.dart:165:13)
<asynchronous suspension>
#31     Invoker._onRun.<anonymous closure>.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:test_api/src/backend/invoker.dart:400:25)
<asynchronous suspension>
#45     _Timer._runTimers (dart:isolate-patch/timer_impl.dart:382:19)
#46     _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:416:5)
#47     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:172:12)
(elided 28 frames from class _FakeAsync, package dart:async, package dart:async-patch, and package stack_trace)

This was caught by the test expectation on the following line:
  file:///path/to/project/search_delegate_test/test/serach_delegate_problem_test.dart line 37
The test description was:
  Second test
════════════════════════════════════════════════════════════════════════════════════════════════════

Test failed. See exception logs above.
The test description was: Second test

1 个答案:

答案 0 :(得分:0)

您正在使用Maps JavaScript API的地址解析服务。 Google Maps JavaScript API中的服务具有每个会话限制,如文档中所述。

  

注意:无论有多少用户共享同一项目,每个用户会话都会应用附加的速率限制。首次加载API时,系统会为您分配初始的请求配额。使用此配额后,API会每秒对其他请求实施速率限制。如果在特定时间段内提出了太多请求,则API返回OVER_QUERY_LIMIT响应代码。

     

每个会话的速率限制禁止将客户端服务用于批处理请求,例如批处理地理编码。对于批处理请求,请使用Geocoding API Web服务。

来源:https://developers.google.com/maps/documentation/javascript/geocoding

据我所知,最初您有10个请求。一旦存储桶为空,请求将被拒绝。桶以每秒1请求的速度重新填充。因此,您必须限制地理编码请求,才能将其限制在每个会话允许的范围内。

您应该检查响应的状态。如果状态为OVER_QUERY_LIMIT,则说明您已耗尽存储桶,需要重试该请求。您可以使用指数退避方法重试逻辑(https://en.wikipedia.org/wiki/Exponential_backoff)。

var zipCode=[...]; //zipCode is array of zip codes.
var delayFactor = 0;

function func1() {
    zipCode.forEach((item, index) => {
        drawZipCodeMarker(item.zip);
    });
}

function drawZipCodeMarker(zip) {
   geocoder.geocode({'address':zip}, (results, status) => {
       if (status === google.maps.GeocoderStatus.OK) {
           console.log(zip);
           console.log(results);
           if (results != null) {
               var temp = new google.maps.Marker({position : results[0].geometry.location, map:map, title:zip});
           }
       } else if (status === google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
           delayFactor++;
           setTimeout(function () {
               drawZipCodeMarker(zip)
           }, delayFactor * 1100);
       } else {
           console.log("Error: " + status);
       }
   });
}

我希望这会有所帮助!