java - 在等待中使用 google-maps-services-java 时出现异常

标签 java google-maps

我只是尝试发出附近的搜索请求,过滤结果,最后对我得到的每个地点发出详细搜索请求。

我做了这样的事情:

    GeoApiContext geoApiContext = new GeoApiContext().setApiKey("my_key");
    NearbySearchRequest nearbySearchRequest = new NearbySearchRequest(geoApiContext);
    PlaceDetailsRequest placeDetailsRequest = new PlaceDetailsRequest(geoApiContext);

    try {
        nearbySearchRequest.location(new LatLng(latitude, longitude)).radius(1000);
        nearbySearchRequest.type(placeType);
        PlacesSearchResponse placesSearchResponse = nearbySearchRequest.await();
        Collections.addAll(places, placesSearchResponse.results);
    } catch (Exception e) {
        e.printStackTrace();
    }
    List<PlacesSearchResult> sortedPoints = filterPlaces(places);
    LOGGER.info(sortedPoints.size());
    try {
        for (PlacesSearchResult result : sortedPoints) {
            placeDetailsRequest.placeId(result.placeId);
            PlaceDetails placeDetails = placeDetailsRequest.await();
            objectOfInterests.add(convert(placeDetails));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

这是打印单元测试的堆栈跟踪:

abr 21, 2016 10:10:39 AM com.google.maps.GeoApiContext getWithPath
INFORMACIÓN: Request: https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=AIza...&location=-0.206238%2C-78.492243&radius=1000&type=lodging
abr 21, 2016 10:10:41 AM com.google.maps.GeoApiContext getWithPath
INFORMACIÓN: Request: https://maps.googleapis.com/maps/api/place/details/json?key=AIza...&placeid=ChIJd6N_BGma1ZERqZqNFkQ7hGU
10:10:41,416 INFO ObjectOfInterestService:77 - 3
java.lang.IllegalStateException: 'await', 'awaitIgnoreError' or 'setCallback' was already called.
    at com.google.maps.PendingResultBase.makeRequest(PendingResultBase.java:74)
    at com.google.maps.PendingResultBase.await(PendingResultBase.java:55)
    at com.guide.services.ObjectOfInterestService.queryGoogleByInterestType(ObjectOfInterestService.java:81)
    at com.guide.services.ObjectOfInterestServiceTest.testQueryGoogleByInterestType(ObjectOfInterestServiceTest.java:20)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

for循环只执行一次,但列表有3个元素。异常停止for循环的执行。

我该如何解决这个问题?

最佳答案

尝试在循环内移动PlaceDetailsRequest。它不可重复使用。

try {
    for (PlacesSearchResult result : sortedPoints) {
        PlaceDetailsRequest placeDetailsRequest = 
            new PlaceDetailsRequest(geoApiContext);
        placeDetailsRequest.placeId(result.placeId);
        PlaceDetails placeDetails = placeDetailsRequest.await();
        objectOfInterests.add(convert(placeDetails));
    }
} catch (Exception e) {
    e.printStackTrace();
}

关于java - 在等待中使用 google-maps-services-java 时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36775172/

相关文章:

java - 如何在JBOSS中部署应用程序?

java - 如何在Android约束布局中均匀分布对象?

objective-c - 如何将格式说明符添加到NSURL

android - 谷歌地图 V2 给出先前触摸位置的地址

本地应用程序和 Jenkins 部署之间的 Java 日期时间不一致

java - 替代 "Referer" header

java - 将不带参数的查询传递给 PreparedStatement 是否安全?

javascript - Google 地理编码 API 状态检查 - OVER_QUERY_LIMIT

javascript - 英国《金融时报》 map 在 Blogger 中无法使用

javascript - 谷歌地图边界不准确