@Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
          /*
          Retrieve the place ID the selected item from the Adapter
          Each Place suggestion is stored as an AutocompletePrediction
          in the Adapter
           */

          final AutocompletePrediction item = mPlaceAdapter.getItem(position);
          final String placeId = item.getPlaceId();
          final CharSequence primaryText = item.getPrimaryText(null);

          Log.i(TAG, "Autocomplete item selected: " + primaryText);

          /*
          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(mGoogleApiClient, placeId);
          placeResult.setResultCallback(mUpdatePlaceDetailsCallback);

          mAutocompleteView.setText(primaryText);

          Log.i(TAG, "Called getPlaceById to get Place details for " + placeId);
        }
  /**
   * Submits an autocomplete query to the Places Geo Data Autocomplete API. Results are returned as
   * frozen AutocompletePrediction objects, ready to be cached. objects to store the Place ID and
   * description that the API returns. Returns an empty list if no results were found. Returns null
   * if the API client is not available or the query did not complete successfully. This method MUST
   * be called off the main UI thread, as it will block until data is returned from the API, which
   * may include a network request.
   *
   * @param constraint Autocomplete query string
   * @return Results from the autocomplete API or null if the query was not successful.
   * @see Places#GEO_DATA_API#getAutocomplete(CharSequence)
   * @see AutocompletePrediction#freeze()
   */
  private ArrayList<AutocompletePrediction> getAutocomplete(CharSequence constraint) {
    if (mGoogleApiClient.isConnected()) {
      Log.i(TAG, "Starting autocomplete query for: " + constraint);

      // Submit the query to the autocomplete API and retrieve a PendingResult that will
      // contain the results when the query completes.
      PendingResult<AutocompletePredictionBuffer> results =
          Places.GeoDataApi.getAutocompletePredictions(
              mGoogleApiClient, constraint.toString(), mBounds, mPlaceFilter);

      // This method should have been called off the main UI thread. Block and wait for at most 60s
      // for a result from the API.
      AutocompletePredictionBuffer autocompletePredictions = results.await(60, TimeUnit.SECONDS);

      // Confirm that the query completed successfully, otherwise return null
      final Status status = autocompletePredictions.getStatus();
      if (!status.isSuccess()) {
        Toast.makeText(
                getContext(), "Error contacting API: " + status.toString(), Toast.LENGTH_SHORT)
            .show();
        Log.e(TAG, "Error getting autocomplete prediction API call: " + status.toString());
        autocompletePredictions.release();
        return null;
      }

      Log.i(
          TAG, "Query completed. Received " + autocompletePredictions.getCount() + " predictions.");

      // Freeze the results immutable representation that can be stored safely.
      return DataBufferUtils.freezeAndClose(autocompletePredictions);
    }
    Log.e(TAG, "Google API client is not connected for autocomplete query.");
    return null;
  }
  private ArrayList<PlaceAutocomplete> getAutocomplete(CharSequence constraint) {
    if (mGoogleApiClient.isConnected()) {
      Log.i(TAG, "Starting autocomplete query for: " + constraint);

      // Submit the query to the autocomplete API and retrieve a PendingResult that will
      // contain the results when the query completes.
      PendingResult<AutocompletePredictionBuffer> results =
          Places.GeoDataApi.getAutocompletePredictions(
              mGoogleApiClient, constraint.toString(), null, mPlaceFilter);

      // This method should have been called off the main UI thread. Block and wait for at most 60s
      // for a result from the API.
      AutocompletePredictionBuffer autocompletePredictions = results.await(60, TimeUnit.SECONDS);

      // Confirm that the query completed successfully, otherwise return null
      final Status status = autocompletePredictions.getStatus();
      if (!status.isSuccess()) {
        Toast.makeText(
                getContext(),
                "Error contacting Google Places: " + status.toString(),
                Toast.LENGTH_SHORT)
            .show();
        Log.e(
            TAG,
            "Error getting autocomplete prediction API call: "
                + status.getStatusMessage()
                + status.getStatus().getStatusMessage());
        autocompletePredictions.release();
        return null;
      }

      Log.i(
          TAG, "Query completed. Received " + autocompletePredictions.getCount() + " predictions.");

      // Copy the results into our own data structure, because we can't hold onto the buffer.
      // AutocompletePrediction objects encapsulate the API response (place ID and description).

      Iterator<AutocompletePrediction> iterator = autocompletePredictions.iterator();
      ArrayList resultList = new ArrayList<>(autocompletePredictions.getCount());
      while (iterator.hasNext()) {
        AutocompletePrediction prediction = iterator.next();
        // Get the details of this prediction and copy it into a new PlaceAutocomplete object.
        resultList.add(new PlaceAutocomplete(prediction.getPlaceId(), prediction.getDescription()));
      }

      // Release the buffer now that all data has been copied.
      autocompletePredictions.release();

      return resultList;
    }
    Log.e(TAG, "Google API client is not connected for autocomplete query.");
    return null;
  }
        @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 = mAdapter.getItem(position);
          final String placeId = String.valueOf(item.placeId);
          Log.i(TAG, "Autocomplete item selected: " + item.description);

          PendingResult<PlaceBuffer> placeResult =
              Places.GeoDataApi.getPlaceById(mGoogleApiClient, placeId);

          placeResult.setResultCallback(mPlacePickedChangeMap);
        }
Пример #5
0
        @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 = mAdapter.getItem(position);
          final String placeId = String.valueOf(item.placeId);
          Log.i("Input place", "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(mGoogleApiClient, placeId);
          placeResult.setResultCallback(mUpdatePlaceDetailsCallback);
          Log.i("Input place", "Called getPlaceById to get Place details for " + item.placeId);
        }
Пример #6
0
  public void getRequestedLocations(String query) {
    mQueryLocationRequested = true;
    mRequest = query;
    Log.i(LOG_TAG, "Define query places");
    final PendingResult<AutocompletePredictionBuffer> predictions =
        Places.GeoDataApi.getAutocompletePredictions(mApiClient, query, null, null);
    predictions.setResultCallback(
        new ResultCallback<AutocompletePredictionBuffer>() {
          @Override
          public void onResult(AutocompletePredictionBuffer autocompletePredictions) {
            boolean resultSuccess = autocompletePredictions.getStatus().isSuccess();
            Log.i(LOG_TAG, "Requested places result success: " + resultSuccess);
            if (resultSuccess) {
              mPlacesList.clear();

              List<String> placesIdList = new ArrayList<>();
              for (AutocompletePrediction prediction : autocompletePredictions) {
                Log.d(LOG_TAG, "Place: " + prediction.toString());
                placesIdList.add(prediction.getPlaceId());
              }
              String[] placesIdArray = new String[placesIdList.size()];
              PendingResult<PlaceBuffer> place =
                  Places.GeoDataApi.getPlaceById(mApiClient, placesIdList.toArray(placesIdArray));
              ResultCallback<PlaceBuffer> callback =
                  new ResultCallback<PlaceBuffer>() {
                    @Override
                    public void onResult(PlaceBuffer places) {
                      for (Place place : places) {
                        addPlaceData(place);
                      }
                      ((OnDataReceivedListener) getActivity()).onDataReceived(mPlacesList);
                    }
                  };
              place.setResultCallback(callback);
            }
            mQueryLocationRequested = false;
            mRequest = null;
            autocompletePredictions.release();
          }
        });
  }