Java 类com.google.android.gms.location.places.AutocompletePrediction 实例源码

项目:MADBike    文件:PlaceAutocompleteAdapter.java   
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = super.getView(position, convertView, parent);

    // Sets the primary and secondary text for a row.
    // Note that getPrimaryText() and getSecondaryText() return a CharSequence that may contain
    // styling based on the given CharacterStyle.

    AutocompletePrediction item = getItem(position);

    TextView textView1 = (TextView) row.findViewById(android.R.id.text1);
    TextView textView2 = (TextView) row.findViewById(android.R.id.text2);
    textView1.setText(item.getPrimaryText(STYLE_BOLD));
    textView2.setText(item.getSecondaryText(STYLE_BOLD));

    return row;
}
项目:MADBike    文件:PlaceAutocompleteAdapter.java   
private ArrayList<AutocompletePrediction> getAutocomplete(CharSequence constraint) {
    if (mGoogleApiClient.isConnected()) {

        PendingResult<AutocompletePredictionBuffer> results =
                Places.GeoDataApi
                        .getAutocompletePredictions(mGoogleApiClient, constraint.toString(),
                                mBounds, mPlaceFilter);

        AutocompletePredictionBuffer autocompletePredictions = results
                .await(60, TimeUnit.SECONDS);

        final Status status = autocompletePredictions.getStatus();
        if (!status.isSuccess()) {
            Toast.makeText(getContext(), "Error contacting API: " + status.toString(),
                    Toast.LENGTH_SHORT).show();
            autocompletePredictions.release();
            return null;
        }
        return DataBufferUtils.freezeAndClose(autocompletePredictions);
    }
    return null;
}
项目:Pickr    文件:DataManager.java   
public Observable<PlacePrediction> getAutocompleteResults(final GoogleApiClient mGoogleApiClient, final String query, final LatLngBounds bounds) {
    return Observable.create(new Observable.OnSubscribe<PlacePrediction>() {
        @Override
        public void call(Subscriber<? super PlacePrediction> subscriber) {

            PendingResult<AutocompletePredictionBuffer> results =
                    Places.GeoDataApi.getAutocompletePredictions(mGoogleApiClient, query,
                            bounds, null);

            AutocompletePredictionBuffer autocompletePredictions = results
                    .await(60, TimeUnit.SECONDS);

            final Status status = autocompletePredictions.getStatus();
            if (!status.isSuccess()) {
                autocompletePredictions.release();
                subscriber.onError(null);
            } else {
                for (AutocompletePrediction autocompletePrediction : autocompletePredictions) {
                    subscriber.onNext(
                            new PlacePrediction(
                                    autocompletePrediction.getPlaceId(),
                                    autocompletePrediction.getDescription()
                            ));
                }
                autocompletePredictions.release();
                subscriber.onCompleted();
            }
        }
    });
}
项目:AutocompleteLocation    文件:AutoCompleteAdapter.java   
private ArrayList<AutocompletePrediction> getAutocomplete(CharSequence constraint) {
  if (mGoogleApiClient.isConnected()) {
    PendingResult<AutocompletePredictionBuffer> results =
        Places.GeoDataApi.getAutocompletePredictions(mGoogleApiClient, constraint.toString(),
            mBounds, mPlaceFilter);

    AutocompletePredictionBuffer autocompletePredictions = results.await(60, TimeUnit.SECONDS);

    final Status status = autocompletePredictions.getStatus();
    if (!status.isSuccess()) {
      Toast.makeText(getContext(), "Error contacting API: " + status.toString(),
          Toast.LENGTH_SHORT).show();
      autocompletePredictions.release();
      return null;
    }

    return DataBufferUtils.freezeAndClose(autocompletePredictions);
  }
  return null;
}
项目:go-jay    文件:PlaceAutoCompleteHelper.java   
private ArrayList<AutocompletePrediction> getAutocomplete(CharSequence constraint) {
    if (mGoogleApiClient.isConnected()) {
        PendingResult<AutocompletePredictionBuffer> results =
                  Places.GeoDataApi
            .getAutocompletePredictions(mGoogleApiClient, constraint.toString(),
                                        mBounds, mPlaceFilter);
                AutocompletePredictionBuffer autocompletePredictions = results
                  .await(60, TimeUnit.SECONDS);
                final Status status = autocompletePredictions.getStatus();
        if (!status.isSuccess()) {
            if (callback != null) callback.onSuggestFail(status);
            autocompletePredictions.release();
            return null;
        }
            return DataBufferUtils.freezeAndClose(autocompletePredictions);
    }
        return null;
}
项目:AutocompleteLocation    文件:AutoCompleteAdapter.java   
private ArrayList<AutocompletePrediction> getAutocomplete(CharSequence constraint) {
  if (mGoogleApiClient.isConnected()) {
    PendingResult<AutocompletePredictionBuffer> results =
        Places.GeoDataApi.getAutocompletePredictions(mGoogleApiClient, constraint.toString(),
            mBounds, mPlaceFilter);

    AutocompletePredictionBuffer autocompletePredictions = results.await(60, TimeUnit.SECONDS);

    final Status status = autocompletePredictions.getStatus();
    if (!status.isSuccess()) {
      Toast.makeText(getContext(), "Error contacting API: " + status.toString(),
          Toast.LENGTH_SHORT).show();
      autocompletePredictions.release();
      return null;
    }

    return DataBufferUtils.freezeAndClose(autocompletePredictions);
  }
  return null;
}
项目:place-search-dialog    文件:PlaceAutocompleteAdapter.java   
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = super.getView(position, convertView, parent);

    // Sets the primary and secondary text for a row.
    // Note that getPrimaryText() and getSecondaryText() return a CharSequence that may contain
    // styling based on the given CharacterStyle.

    AutocompletePrediction item = getItem(position);

    TextView textView1 = (TextView) row.findViewById(android.R.id.text1);
    TextView textView2 = (TextView) row.findViewById(android.R.id.text2);
    textView1.setText(item.getPrimaryText(STYLE_BOLD));
    textView2.setText(item.getSecondaryText(STYLE_BOLD));

    return row;
}
项目:Akwukwo    文件:PlaceAutocompleteAdapter.java   
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = super.getView(position, convertView, parent);

    // Sets the primary and secondary text for a row.
    // Note that getPrimaryText() and getSecondaryText() return a CharSequence that may contain
    // styling based on the given CharacterStyle.

    AutocompletePrediction item = getItem(position);

    try{
        TextView textView1 = (TextView) row.findViewById(android.R.id.text1);
        TextView textView2 = (TextView) row.findViewById(android.R.id.text2);
        textView1.setText(item.getPrimaryText(STYLE_BOLD));
        textView2.setText(item.getSecondaryText(STYLE_BOLD));
    } catch (Exception e){
        e.printStackTrace();
    }

    return row;
}
项目:Akwukwo    文件:SearchLocationActivity.java   
@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 AutocompletePrediction from which we
     read the place ID and title.
      */
    final AutocompletePrediction item = mAdapter.getItem (position);
    final String placeId = item.getPlaceId ();
    final CharSequence primaryText = item.getPrimaryText (null);
    Log.i("", "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);

    Log.i("", "Called getPlaceById to get Place details for " + placeId);
    mSearchLocation.setThreshold(1000);
}
项目:ExamplesAndroid    文件:PlaceAutocompleteAdapter.java   
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = super.getView(position, convertView, parent);

    // Sets the primary and secondary text for a row.
    // Note that getPrimaryText() and getSecondaryText() return a CharSequence that may contain
    // styling based on the given CharacterStyle.

    AutocompletePrediction item = getItem(position);

    TextView textView1 = (TextView) row.findViewById(android.R.id.text1);
    TextView textView2 = (TextView) row.findViewById(android.R.id.text2);
    textView1.setText(item.getPrimaryText(STYLE_BOLD));
    textView2.setText(item.getSecondaryText(STYLE_BOLD));

    return row;
}
项目:ExamplesAndroid    文件:MainActivity.java   
@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 AutocompletePrediction from which we
     read the place ID and title.
      */
    final AutocompletePrediction item = mAdapter.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);

    Toast.makeText(getApplicationContext(), "Clicked: " + primaryText,
            Toast.LENGTH_SHORT).show();
    Log.i(TAG, "Called getPlaceById to get Place details for " + placeId);
}
项目:Bikeable    文件:PlaceAutocompleteAdapter.java   
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = super.getView(position, convertView, parent);

    // Sets the primary and secondary text for a row.
    // Note that getPrimaryText() and getSecondaryText() return a CharSequence that may contain
    // styling based on the given CharacterStyle.

    AutocompletePrediction item = getItem(position);

    TextView textView1 = (TextView) row.findViewById(android.R.id.text1);
    TextView textView2 = (TextView) row.findViewById(android.R.id.text2);
    textView1.setText(item.getPrimaryText(STYLE_BOLD));
    textView2.setText(item.getSecondaryText(STYLE_BOLD));

    return row;
}
项目:Bikeable    文件:SearchHistoryCollector.java   
private void updateToOfflineHistory() {

        FileOutputStream outputStream;

        try {
            // delete file content
            PrintWriter writer = new PrintWriter(historyFile);
            writer.print("");
            writer.close();

            outputStream = appContext.openFileOutput(historyFileName, Context.MODE_APPEND); //todo MODE_PRIVATE ?
            for (AutocompletePrediction prediction : onlineHistory){
                outputStream.write(prediction.getPlaceId().concat("\n").getBytes());
            }
            outputStream.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
项目:Bikeable    文件:ClearableAutoCompleteTextView.java   
private void showOnlyFixedResults() {
    PlaceAutocompleteAdapter currAdapter = (PlaceAutocompleteAdapter) currView.getAdapter();
    ArrayList<AutocompletePrediction> fixedResults = new ArrayList();
    fixedResults.addAll(currAdapter.getFixedResults());
    if (searchHistoryCollector != null && !searchHistoryCollector.getOnlineHistory().isEmpty()){
        fixedResults.addAll(searchHistoryCollector.getOnlineHistory());
    }

    currAdapter.setResultsList(fixedResults);
    currView.getHandler().postDelayed(new Runnable() {
        @Override
        public void run() {
            currView.showDropDown();
        }
    }, 500);
}
项目:android-play-places    文件:PlaceAutocompleteAdapter.java   
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = super.getView(position, convertView, parent);

    // Sets the primary and secondary text for a row.
    // Note that getPrimaryText() and getSecondaryText() return a CharSequence that may contain
    // styling based on the given CharacterStyle.

    AutocompletePrediction item = getItem(position);

    TextView textView1 = (TextView) row.findViewById(android.R.id.text1);
    TextView textView2 = (TextView) row.findViewById(android.R.id.text2);
    textView1.setText(item.getPrimaryText(STYLE_BOLD));
    textView2.setText(item.getSecondaryText(STYLE_BOLD));

    return row;
}
项目:android-play-places    文件:MainActivity.java   
@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 AutocompletePrediction from which we
     read the place ID and title.
      */
    final AutocompletePrediction item = mAdapter.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 Client to retrieve a Place object with
     additional details about the place.
      */
    Task<PlaceBufferResponse> placeResult = mGeoDataClient.getPlaceById(placeId);
    placeResult.addOnCompleteListener(mUpdatePlaceDetailsCallback);

    Toast.makeText(getApplicationContext(), "Clicked: " + primaryText,
            Toast.LENGTH_SHORT).show();
    Log.i(TAG, "Called getPlaceById to get Place details for " + placeId);
}
项目:Android-HankeiN    文件:AddressAutocompleAdapter.java   
void performQuery(final String query) {
    Timber.d("performQuery: %s", query);

    placeEngine.queryAutocompletion(query)
            .subscribe(new Subscriber<Iterable<AutocompletePrediction>>() {
                @Override
                public void onCompleted() {

                }

                @Override
                public void onError(Throwable e) {
                    Timber.w(e, "failed to queryAutocompletion queryAutocompletion for: %s", query);
                }

                @Override
                public void onNext(Iterable<AutocompletePrediction> predictions) {
                    clear();
                    addAll(convertToSpannedList(predictions));
                }
            });

}
项目:MADBike    文件:SearchActivity.java   
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    AnalyticsManager.getInstance().trackSearch();
    final AutocompletePrediction item = mAdapter.getItem(position);
    final String placeId = item.getPlaceId();
    final CharSequence primaryText = item.getPrimaryText(null);
    PendingResult<PlaceBuffer> placeResult = Places.GeoDataApi
            .getPlaceById(mGoogleApiClient, placeId);
    placeResult.setResultCallback(mUpdatePlaceDetailsCallback);
}
项目:AutocompleteLocation    文件:AutoCompleteLocation.java   
@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  UIUtils.hideKeyboard(AutoCompleteLocation.this.getContext(), AutoCompleteLocation.this);
  final AutocompletePrediction item = mAutoCompleteAdapter.getItem(position);
  if (item != null) {
    final String placeId = item.getPlaceId();
    PendingResult<PlaceBuffer> placeResult =
        Places.GeoDataApi.getPlaceById(mGoogleApiClient, placeId);
    placeResult.setResultCallback(mUpdatePlaceDetailsCallback);
  }
}
项目:AutocompleteLocation    文件:AutoCompleteAdapter.java   
@Override public View getView(int position, View convertView, ViewGroup parent) {
  View row = super.getView(position, convertView, parent);
  AutocompletePrediction item = getItem(position);
  TextView textView1 = (TextView) row.findViewById(android.R.id.text1);
  TextView textView2 = (TextView) row.findViewById(android.R.id.text2);
  textView1.setText(item.getPrimaryText(STYLE_BOLD));
  textView2.setText(item.getSecondaryText(STYLE_BOLD));

  return row;
}
项目:go-jay    文件:PlaceAutoCompleteHelper.java   
@Override
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    final AutocompletePrediction item = mAdapter.getItem(position);
          final String placeId = item.getPlaceId();
    PendingResult<PlaceBuffer> placeResult = Places.GeoDataApi
        .getPlaceById(mGoogleApiClient, placeId);
          placeResult.setResultCallback(mUpdatePlaceDetailsCallback);
}
项目:go-jay    文件:PlaceAutoCompleteHelper.java   
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = super.getView(position, convertView, parent);
    AutocompletePrediction item = getItem(position);
        TextView textView1 = (TextView) row.findViewById(R.id.place1);
    TextView textView2 = (TextView) row.findViewById(R.id.place2);
    ImageView img=(ImageView) row.findViewById(R.id.itemplaceImageView1);
    textView1.setText(item.getPrimaryText(STYLE_BOLD));
    textView2.setText(item.getFullText(STYLE_BOLD));
    img.setColorFilter(android.graphics.Color.GRAY);
            return row;
}
项目:moneytracking    文件:PlaceAdapter.java   
private ArrayList<PlaceAutocomplete> getPredictions(CharSequence constraint) {
    if (mGoogleApiClient != null) {
        Log.i(TAG, "Executing autocomplete query for: " + constraint);
        PendingResult<AutocompletePredictionBuffer> results =
                Places.GeoDataApi
                        .getAutocompletePredictions(mGoogleApiClient, constraint.toString(),
                                mBounds, mPlaceFilter);
        // Wait for predictions, set the timeout.
        AutocompletePredictionBuffer autocompletePredictions = results
                .await(60, TimeUnit.SECONDS);
        final Status status = autocompletePredictions.getStatus();
        if (!status.isSuccess()) {
            Toast.makeText(getContext(), "We recommend using internet for better accuracy of the location! ", Toast.LENGTH_SHORT).show();

            Log.e(TAG, "Error getting place predictions: " + status.toString());
            autocompletePredictions.release();
            return null;
        }

        Log.i(TAG, "Query completed. Received " + autocompletePredictions.getCount()
                + " predictions.");
        Iterator<AutocompletePrediction> iterator = autocompletePredictions.iterator();
        ArrayList resultList = new ArrayList<>(autocompletePredictions.getCount());
        while (iterator.hasNext()) {
            AutocompletePrediction prediction = iterator.next();
            resultList.add(new PlaceAutocomplete(prediction.getPlaceId(),
                    prediction.getFullText(null)));
        }
        // Buffer release
        autocompletePredictions.release();
        return resultList;
    }
    Log.e(TAG, "Google API client is not connected.");
    return null;
}
项目:MapsWithPlacesAutoComplete    文件:AutoCompleteAdapter.java   
/**
 * Method to call API for each user input
 * @param constraint User input character string
 * @return ArrayList containing suggestion results
 */
private ArrayList<PlaceAutoComplete> getAutoComplete(CharSequence constraint){
    if(mGoogleApiClient.isConnected()){
        //Making a query and fetching result in a pendingResult

        PendingResult<AutocompletePredictionBuffer> results= Places.GeoDataApi
                .getAutocompletePredictions(mGoogleApiClient,constraint.toString(),mBounds,mPlaceFilter);

        //Block and wait for 60s for a result
        AutocompletePredictionBuffer autocompletePredictions=results.await(60, TimeUnit.SECONDS);

        final Status status=autocompletePredictions.getStatus();

        // Confirm that the query completed successfully, otherwise return null
        if(!status.isSuccess()){
            Log.e(TAG, "Error getting autocomplete prediction API call: " + status.toString());
            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();
            resultList.add(new PlaceAutoComplete(prediction.getPlaceId(),prediction.getPrimaryText(null),prediction.getSecondaryText(null)));
        }
        autocompletePredictions.release();
        return resultList;
    }else{
        Log.e(TAG,"GoogleApiClient Not Connected");
        return  null;
    }
}
项目:directly    文件:LocationRepositoryImpl.java   
@Override
public Observable<Toilet> initPlaceId(Location currentLocation, Toilet toilet) {
    String requestString = (toilet.getCity() + ", " + toilet.getAddress()).replace(", б/н", "");
    if (toilet.getLatitude() != 0) {
        return Observable.just(toilet);
    }
    return locationProvider.getPlaceAutocompletePredictions(requestString, null, null)
            .map(autocompletePredictions -> {
                String placeId = null;
                for (AutocompletePrediction prediction : autocompletePredictions) {
                    if (prediction.getSecondaryText(null).toString().contains(toilet.getCity() + ",")) {
                        placeId = prediction.getPlaceId();
                        break;
                    } else {
                        Timber.e("Location prediction fail: %s", prediction.getSecondaryText(null));
                    }
                }
                toilet.setPlaceId(placeId);
                return toilet;
            })
            .flatMap(toilet1 -> toilet.getPlaceId() == null ? Observable.just(toilet1) : locationProvider.getPlaceById(toilet1.getPlaceId()).flatMap(places -> {
                LatLng latLng = places.get(0).getLatLng();
                if (latLng != null) {
                    toilet1.setLatitude(latLng.latitude);
                    toilet1.setLongitude(latLng.longitude);
                }
                return Observable.just(toilet1);
            }))
            .subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread());
}
项目:movemate_android    文件:PlaceArrayAdapter.java   
private ArrayList<PlaceAutocomplete> getPredictions(CharSequence constraint) {
    if (mGoogleApiClient != null) {
        Log.i(TAG, "Executing autocomplete query for: " + constraint);
        PendingResult<AutocompletePredictionBuffer> results =
                Places.GeoDataApi
                        .getAutocompletePredictions(mGoogleApiClient, constraint.toString(),
                                mBounds, mPlaceFilter);
        // Wait for predictions, set the timeout.
        AutocompletePredictionBuffer autocompletePredictions = results
                .await(60, TimeUnit.SECONDS);
        final Status status = autocompletePredictions.getStatus();
        if (!status.isSuccess()) {
            Toast.makeText(getContext(), "Error: " + status.toString(),
                    Toast.LENGTH_SHORT).show();
            Log.e(TAG, "Error getting place predictions: " + status
                    .toString());
            autocompletePredictions.release();
            return null;
        }

        Log.i(TAG, "Query completed. Received " + autocompletePredictions.getCount()
                + " predictions.");
        Iterator<AutocompletePrediction> iterator = autocompletePredictions.iterator();
        ArrayList resultList = new ArrayList<>(autocompletePredictions.getCount());
        while (iterator.hasNext()) {
            AutocompletePrediction prediction = iterator.next();
            resultList.add(new PlaceAutocomplete(prediction.getPlaceId(),
                    prediction.getFullText(null)));
        }
        // Buffer release
        autocompletePredictions.release();
        return resultList;
    }
    Log.e(TAG, "Google API client is not connected.");
    return null;
}
项目:AutocompleteLocation    文件:AutoCompleteLocation.java   
@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  UIUtils.hideKeyboard(AutoCompleteLocation.this.getContext(), AutoCompleteLocation.this);
  final AutocompletePrediction item = mAutoCompleteAdapter.getItem(position);
  if (item != null) {
    final String placeId = item.getPlaceId();
    PendingResult<PlaceBuffer> placeResult =
        Places.GeoDataApi.getPlaceById(mGoogleApiClient, placeId);
    placeResult.setResultCallback(mUpdatePlaceDetailsCallback);
  }
}
项目:AutocompleteLocation    文件:AutoCompleteAdapter.java   
@Override public View getView(int position, View convertView, ViewGroup parent) {
  View row = super.getView(position, convertView, parent);
  AutocompletePrediction item = getItem(position);
  TextView textView1 = (TextView) row.findViewById(android.R.id.text1);
  TextView textView2 = (TextView) row.findViewById(android.R.id.text2);
  textView1.setText(item.getPrimaryText(STYLE_BOLD));
  textView2.setText(item.getSecondaryText(STYLE_BOLD));

  return row;
}
项目:place-search-dialog    文件:PlaceAutocompleteAdapter.java   
/**
 * 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;
}
项目:place-search-dialog    文件:PlaceSearchDialog.java   
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

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

    //Hide Keyboard
    InputMethodManager in = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    in.hideSoftInputFromWindow(view.getWindowToken(), 0);

    Log.i(TAG, "Autocomplete item selected: " + primaryText);
}
项目:Akwukwo    文件:PlaceAutocompleteAdapter.java   
/**
     * 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 statusComplete
     * 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()) {
//
//            constraint = constraint  + " " +mConstraint;
            // 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(), "Check your internet connection", Toast.LENGTH_SHORT).show();
                autocompletePredictions.release();
                return null;
            }


            // Freeze the results immutable representation that can be stored safely.
            return DataBufferUtils.freezeAndClose(autocompletePredictions);
        }
        return null;
    }
项目:PlaceAPIAutocomplete    文件:PlaceArrayAdapter.java   
private ArrayList<PlaceAutocomplete> getPredictions(CharSequence constraint) {
    if (mGoogleApiClient != null) {
        Log.i(TAG, "Executing autocomplete query for: " + constraint);
        PendingResult<AutocompletePredictionBuffer> results =
                Places.GeoDataApi
                        .getAutocompletePredictions(mGoogleApiClient, constraint.toString(),
                                mBounds, mPlaceFilter);
        // Wait for predictions, set the timeout.
        AutocompletePredictionBuffer autocompletePredictions = results
                .await(60, TimeUnit.SECONDS);
        final Status status = autocompletePredictions.getStatus();
        if (!status.isSuccess()) {
            Toast.makeText(getContext(), "Error: " + status.toString(),
                    Toast.LENGTH_SHORT).show();
            Log.e(TAG, "Error getting place predictions: " + status
                    .toString());
            autocompletePredictions.release();
            return null;
        }

        Log.i(TAG, "Query completed. Received " + autocompletePredictions.getCount()
                + " predictions.");
        Iterator<AutocompletePrediction> iterator = autocompletePredictions.iterator();
        ArrayList resultList = new ArrayList<>(autocompletePredictions.getCount());
        while (iterator.hasNext()) {
            AutocompletePrediction prediction = iterator.next();
            resultList.add(new PlaceAutocomplete(prediction.getPlaceId(),
                    prediction.getFullText(null)));
        }
        // Buffer release
        autocompletePredictions.release();
        return resultList;
    }
    Log.e(TAG, "Google API client is not connected.");
    return null;
}
项目:ExamplesAndroid    文件:PlaceAutocompleteAdapter.java   
/**
 * 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()) {

        // 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();
            autocompletePredictions.release();
            return null;
        }


        // Freeze the results immutable representation that can be stored safely.
        return DataBufferUtils.freezeAndClose(autocompletePredictions);
    }
    return null;
}
项目:Bikeable    文件:PlaceAutocompleteAdapter.java   
/**
     * 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;
    }
项目:Bikeable    文件:DirectionsManager.java   
public void setNewMarkerByPlacePrediction(boolean isFrom, AutocompletePrediction prediction) {

        PlaceDetails placeDetails = null;
        try {
            Log.i("INFO:", String.format("attemp to get place details: place ID: %s", prediction.getPlaceId()));
            placeDetails = PlacesApi.placeDetails(context, prediction.getPlaceId()).await();
        } catch (Exception e) {
            e.printStackTrace(); // todo: handle this properly
        }
        LatLng markerLatLng = MapUtils.getGmsLatLngFromModel(placeDetails.geometry.location);
        String markerTitle = prediction.getDescription();
        Marker newMarker = mMap.addMarker(new MarkerOptions().position(markerLatLng).title(markerTitle));
        if (isFrom){
            if (this.getFromMarkerNew() != null){
                this.getFromMarkerNew().remove();
            }
            if (this.fromMarkerCurr != null){
                this.fromMarkerCurr.remove();
            }
            this.setFromMarkerNew(newMarker);
            this.fromLatLngNew = markerLatLng;
            this.fromTitleNew = prediction.getDescription();
            predictionFrom = prediction;
        }
        else{ // isTo
            if (this.getToMarkerNew() != null){
                this.getToMarkerNew().remove();
            }
            if (this.toMarkerCurr != null){
                this.toMarkerCurr.remove();
            }
            this.setToMarkerNew(newMarker);
            this.toLatLngNew = markerLatLng;
            this.toTitleNew = prediction.getDescription();
            predictionTo = prediction;
        }
    }
项目:Bikeable    文件:SearchHistoryCollector.java   
public void addPredictionToHistory(AutocompletePrediction prediction){
    // will be used after search
    if (prediction == null || prediction.getPlaceId() == null){
        return;
    }

    boolean isInHistory = isPredictionInOnlineHistory(prediction);
    if (isInHistory) {
        return;
    }

    addPredictionToOnlineHistory(prediction);

    updateToOfflineHistory();
}
项目:Bikeable    文件:SearchHistoryCollector.java   
private void addPredictionToOnlineHistory(AutocompletePrediction prediction){
    onlineHistory.addFirst(prediction);
    if (onlineHistory.size() > MAX_HISTORY_RECORDS){
        onlineHistory.removeLast();

    }
}
项目:Bikeable    文件:SearchHistoryCollector.java   
private boolean isPredictionInOnlineHistory(AutocompletePrediction prediction){
    Log.i("INFO:", String.format("online history size: %d", onlineHistory.size()));
    for (AutocompletePrediction onlinePrediction: onlineHistory){
        if (onlinePrediction.getPlaceId().equals(prediction.getPlaceId())){
            return true;
        }
    }
    return false;
}
项目:AndroidDemoProjects    文件:AutoCompleteAdapter.java   
private void displayPredictiveResults( String query )
{
    //Southwest corner to Northeast corner.
    LatLngBounds bounds = new LatLngBounds( new LatLng( 39.906374, -105.122337 ), new LatLng( 39.949552, -105.068779 ) );

    //Filter: https://developers.google.com/places/supported_types#table3
    List<Integer> filterTypes = new ArrayList<Integer>();
    filterTypes.add( Place.TYPE_ESTABLISHMENT );

    Places.GeoDataApi.getAutocompletePredictions( mGoogleApiClient, query, bounds, AutocompleteFilter.create( filterTypes ) )
        .setResultCallback (
            new ResultCallback<AutocompletePredictionBuffer>() {
                @Override
                public void onResult( AutocompletePredictionBuffer buffer ) {

                    if( buffer == null )
                        return;

                    if( buffer.getStatus().isSuccess() ) {
                        for( AutocompletePrediction prediction : buffer ) {
                            //Add as a new item to avoid IllegalArgumentsException when buffer is released
                            add( new AutoCompletePlace( prediction.getPlaceId(), prediction.getDescription() ) );
                        }
                    }

                    //Prevent memory leak by releasing buffer
                    buffer.release();
                }
            }, 60, TimeUnit.SECONDS );
}
项目:Android-HankeiN    文件:AddressAutocompleAdapter.java   
List<CharSequence> convertToSpannedList(Iterable<AutocompletePrediction> predictions) {
    List<CharSequence> list = new ArrayList<>();

    for (AutocompletePrediction prediction : predictions) {
        list.add(prediction.getFullText(BOLD));
    }
    return list;
}