com.google.android.gms.location.places.AutocompletePrediction Java Examples
The following examples show how to use
com.google.android.gms.location.places.AutocompletePrediction.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MainActivity.java From android-play-places with Apache License 2.0 | 6 votes |
@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); }
Example #2
Source File: PlaceAutocompleteAdapter.java From android-play-places with Apache License 2.0 | 6 votes |
@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; }
Example #3
Source File: AutoCompleteAdapter.java From AutocompleteLocation with Apache License 2.0 | 6 votes |
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; }
Example #4
Source File: MainActivity.java From ExamplesAndroid with Apache License 2.0 | 6 votes |
@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); }
Example #5
Source File: PlaceAutocompleteAdapter.java From place-search-dialog with Apache License 2.0 | 6 votes |
@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; }
Example #6
Source File: PlaceAutocompleteAdapter.java From ExamplesAndroid with Apache License 2.0 | 6 votes |
@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; }
Example #7
Source File: AutoCompleteLocation.java From AutocompleteLocation with Apache License 2.0 | 5 votes |
@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); } }
Example #8
Source File: AutoCompleteAdapter.java From AndroidDemoProjects with Apache License 2.0 | 5 votes |
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 ); }
Example #9
Source File: PlaceAutocompleteAdapter.java From ExamplesAndroid with Apache License 2.0 | 5 votes |
/** * 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; }
Example #10
Source File: PlaceSearchDialog.java From place-search-dialog with Apache License 2.0 | 5 votes |
@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); }
Example #11
Source File: PlaceAutocompleteAdapter.java From place-search-dialog with Apache License 2.0 | 5 votes |
/** * 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; }
Example #12
Source File: AutoCompleteAdapter.java From AutocompleteLocation with Apache License 2.0 | 5 votes |
@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; }
Example #13
Source File: PlaceAutocompleteAdapter.java From Airbnb-Android-Google-Map-View with MIT License | 4 votes |
/** * Submits an autocomplete query to the Places Geo Data Autocomplete API. * 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) */ private ArrayList<PlaceAutocomplete> 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()) { 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; }
Example #14
Source File: PlaceAutocompleteAdapter.java From ExamplesAndroid with Apache License 2.0 | 4 votes |
/** * Returns an item from the last autocomplete query. */ @Override public AutocompletePrediction getItem(int position) { return mResultList.get(position); }
Example #15
Source File: PlaceAutocompleteAdapter.java From place-search-dialog with Apache License 2.0 | 4 votes |
/** * Returns an item from the last autocomplete query. */ @Override public AutocompletePrediction getItem(int position) { return mResultList.get(position); }
Example #16
Source File: PlaceAutocompleteAdapter.java From android-play-places with Apache License 2.0 | 4 votes |
/** * Returns an item from the last autocomplete query. */ @Override public AutocompletePrediction getItem(int position) { return mResultList.get(position); }
Example #17
Source File: AutoCompleteAdapter.java From AutocompleteLocation with Apache License 2.0 | 4 votes |
@Override public AutocompletePrediction getItem(int position) { return mResultList.get(position); }
Example #18
Source File: PlacesAutoCompleteAdapter.java From GoogleAutoCompleteWithRecyclerView with GNU General Public License v2.0 | 4 votes |
private ArrayList<PlaceAutocomplete> getAutocomplete(CharSequence constraint) { if (mGoogleApiClient.isConnected()) { Log.i("", "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(mContext, "Error contacting API: " + status.toString(), Toast.LENGTH_SHORT).show(); Log.e("", "Error getting autocomplete prediction API call: " + status.toString()); autocompletePredictions.release(); return null; } Log.i("", "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("", "Google API client is not connected for autocomplete query."); return null; }
Example #19
Source File: SuggestionsProvider.java From Nibo with MIT License | 4 votes |
public Observable<Collection<NiboSearchSuggestionItem>> getSuggestions(final String query) { final List<NiboSearchSuggestionItem> placeSuggestionItems = new ArrayList<>(); if (mGoogleApiClient == null) { Log.d(TAG, "Google play services cannot be null"); } return new Observable<Collection<NiboSearchSuggestionItem>>() { @Override protected void subscribeActual(final Observer<? super Collection<NiboSearchSuggestionItem>> observer) { Places.GeoDataApi.getAutocompletePredictions(mGoogleApiClient, query, null, null) .setResultCallback( new ResultCallback<AutocompletePredictionBuffer>() { @Override public void onResult(@NonNull AutocompletePredictionBuffer buffer) { placeSuggestionItems.clear(); if (buffer.getStatus().isSuccess()) { Log.d(TAG, buffer.toString() + " " + buffer.getCount()); for (AutocompletePrediction prediction : buffer) { Log.d(TAG, prediction.getFullText(null).toString()); //Add as a new item to avoid IllegalArgumentsException when buffer is released NiboSearchSuggestionItem placeSuggestion = new NiboSearchSuggestionItem( prediction.getFullText(null).toString(), prediction.getPlaceId(), NiboSearchSuggestionItem.TYPE_SEARCH_ITEM_SUGGESTION, mContext.getResources().getDrawable(R.drawable.ic_map_marker_def) ); placeSuggestionItems.add(placeSuggestion); } observer.onNext(placeSuggestionItems); } else { Log.d(TAG, buffer.toString()); observer.onError(new Throwable(buffer.getStatus().getStatusMessage())); } //Prevent memory leak by releasing buffer buffer.release(); } }, 60, TimeUnit.SECONDS); } }; }