【问题标题】:Fail to obtain specific Place from PlaceBuffer从 PlaceBuffer 获取特定 Place 失败
【发布时间】:2015-06-05 06:33:06
【问题描述】:

我对一个特定的地方有奇怪的问题。有什么想法吗?

此代码运行良好,但是对于一个地方(由于以 aaa 开头而意外选择 - 仅用于测试)它无法从缓冲区获取 Place 对象。甚至查询成功(日志:PlaceAutocompleteAdapt:查询已完成。收到 5 个预测。) 不幸的是,它使应用程序崩溃。它使这个 API 非常危险,在 try catch 块中还应该处理什么?

        addressAutocompleteText.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            /*
             Retrieve the place ID of the selected item from the Adapter.
             The adapter stores each Place suggestion in a PlaceAutocomplete object from which we
             read the place ID.
              */
            final PlaceAutocompleteAdapter.PlaceAutocomplete item = ((MainActivity) getActivity()).mAdapter.getItem(position);
            final String placeId = String.valueOf(item.placeId);
            Log.i(TAG, "Autocomplete item selected: " + item.description);

            /* Issue a request to the Places Geo Data API to retrieve a Place object with additional details about the place. */
            PendingResult<PlaceBuffer> placeResult = Places.GeoDataApi.getPlaceById(((MainActivity) getActivity()).mGoogleApiClient, placeId);
            placeResult.setResultCallback(new ResultCallback<PlaceBuffer>() {
                @Override
                public void onResult(PlaceBuffer places) {
                    if (!places.getStatus().isSuccess()) {
                        // Request did not complete successfully
                        Log.e(TAG, "Place query did not complete. Error: " + places.getStatus().toString());
                        places.release();
                        return;
                    }

                    try {
                        // Get the Place object from the buffer.
                        final Place place = places.get(0); // Throws IllegalStateException
                        if (null != place) {
                            coordinate = new Coordinate(place.getLatLng().latitude, place.getLatLng().longitude);
                        }
                    } catch (IllegalStateException e) {
                        Log.w(TAG, "Fail to get place or its coordinates");
                    }

                    places.release();
                }
            });
//                hideKeyboard(addressAutocompleteText);

            Log.i(TAG, "Called getPlaceById to get Place details for " + item.placeId);
        }
    });

日志:

Starting autocomplete query for: aaa
06-05 08:05:29.618    6403-7777/...I/PlaceAutocompleteAdapt﹕ Query completed. Received 5 predictions.
06-05 08:05:39.248    6403-6403/...I/FavoriteLocationCreateFragment﹕ Autocomplete item selected: AAA Auto A.s., Zlín, Česká republika
06-05 08:06:01.278    6403-6403/...I/FavoriteLocationCreateFragment﹕ Called getPlaceById to get Place details for ChIJcYm-YVpzE0cR-OIfKj2KTKc
06-05 08:30:46.823  19727-19727/... D/AndroidRuntime﹕ Shutting down VM
06-05 08:30:46.833  19727-19727/... E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: ..., PID: 19727
    java.lang.IllegalStateException
            at com.google.android.gms.common.internal.zzv.zzP(Unknown Source)
            at com.google.android.gms.common.data.zzc.zzaB(Unknown Source)
            at com.google.android.gms.common.data.zzc.<init>(Unknown Source)
            at com.google.android.gms.location.places.internal.zzq.<init>(Unknown Source)
            at com.google.android.gms.location.places.internal.zzo.<init>(Unknown Source)
            at com.google.android.gms.location.places.PlaceBuffer.get(Unknown Source)
            at ....FavoriteLocationCreateFragment$3$1.onResult(FavoriteLocationCreateFragment.java:129)
            at ....FavoriteLocationCreateFragment$3$1.onResult(FavoriteLocationCreateFragment.java:117)
            at com.google.android.gms.common.api.AbstractPendingResult$CallbackHandler.deliverResultCallback(Unknown Source)
            at com.google.android.gms.common.api.AbstractPendingResult$CallbackHandler.handleMessage(Unknown Source)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5832)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

【问题讨论】:

  • 什么是 PlaceAutocomplete?您可能会从那里得到错误的 ID。我尝试从您的日志消息“ChIJcYm-YVpzE0cR-OIfKj2KTKc”中测试 ID,但没有得到任何结果。好像没有那个ID的地方。在这种情况下,API 会为您提供一个位置缓冲区,其中包含零个结果。如果您调用 places.get(0),您将尝试检索不存在的内容。这个理论的问题在于,在这种情况下,places.getStatus().isSuccess() 应该返回 false,你已经检查过了。考虑记录 places.getCount() 以帮助调试。
  • PlaceAutocomplete 是来自 google 的 API developers.google.com/places/android/autocomplete 是的,placeID 返回空结果。但是为什么我把它放在第一位呢?当状态为 SUCCESS 且计数为 5 时(日志:收到 5 个预测。)。缓冲区怎么可能是空的?这似乎是一个错误)-:
  • 确保将 .addApi(Places.GEO_DATA_API) 添加到 GoogleApiClient
  • 我有同样讨厌的错误。我确定它在 Google 的代码上。
  • 除了try catch还有什么办法吗?

标签: android google-places-api google-places


【解决方案1】:

添加条件&amp;&amp; places.getCount() &gt; 0

文档示例:

Places.GeoDataApi.getPlaceById(mGoogleApiClient, placeId)
                 .setResultCallback(new ResultCallback<PlaceBuffer>() {
        @Override
        public void onResult(PlaceBuffer places) {
            if (places.getStatus().isSuccess() && places.getCount() > 0) {
            final Place myPlace = places.get(0);
            Log.i(TAG, "Place found: " + myPlace.getName());
        } else {
            Log.e(TAG, "Place not found");
        }
        places.release();
    }
});

【讨论】:

    【解决方案2】:

    还需要places.getCount() 检查。请参考这里https://developers.google.com/places/android-api/place-details

    【讨论】:

      【解决方案3】:

      我也遇到了同样的问题。

      我从item.getPlaceId() 方法得到Place ID: ChIJD1eejrW35zsRDFfkT-PSbm4,但是当我做Reverse Geocoding by Place ID 时,我没有得到任何结果。

      可能有很多地方返回与此相同的结果,所以我希望谷歌能够认识到这个问题。

      但现在我已经这样做了:

      /**
       * Callback for results from a Places Geo Data API query that shows the first place result in
       * the details view on screen.
       */
      private ResultCallback<PlaceBuffer> fromPlaceResultCallback = new ResultCallback<PlaceBuffer>() {
          @Override
          public void onResult(@NonNull PlaceBuffer places) {
              if (!places.getStatus().isSuccess()) {
                  places.release();
                  return;
              }
              // Get the Place object from the buffer.
              if(places.getCount() > 0) {
                  // Got place details
                  Place place = places.get(0);
                  // Do your stuff
              } else {
                  // No place details 
                  Toast.makeText(SelectLocation.this, "Place details not found.", Toast.LENGTH_LONG).show();
              }
              places.release();
          }
      };
      

      【讨论】:

        【解决方案4】:

        如果 placeId 在通话中错误/无效,我会遇到这种情况

        Places.GeoDataApi.getPlaceById(((MainActivity) getActivity()).mGoogleApiClient, placeId);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-11-06
          • 1970-01-01
          • 1970-01-01
          • 2019-07-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多