GeoDataApi.getAutocompletePredictions无效

时间:2015-07-26 19:00:46

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

我正在构建一个显示自动完成功能的Android应用程序,并使用--GeoDataApi.getAutocompletePredictions在谷歌地图中获取自动完成预测。我按照本教程 - https://github.com/googlesamples/android-play-places/blob/master/PlaceComplete/Application/src/main/java/com/example/google/playservices/placecomplete/PlaceAutocompleteAdapter.java

但不知何故,这对我来说不太好。

我的课是这个 -

public class GooglePlacesAutoCompleteAdapter extends ArrayAdapter implements Filterable {

    private ArrayList<PlaceAutocomplete> mResultList;
    GoogleApiClient mGoogleApiClient;
    private LatLngBounds mBounds;
    private AutocompleteFilter mPlaceFilter;
    int radius = 500;

    public GooglePlacesAutoCompleteAdapter(Context context, int textViewResourceId, GoogleApiClient googleApiClient,
                                           Location lastLocation, AutocompleteFilter filter) {
        super(context, textViewResourceId);
        LatLng currentLatLng = new LatLng(lastLocation.getLatitude(), lastLocation.getLongitude());
        mBounds = Utility.boundsWithCenterAndLatLngDistance(currentLatLng, 500, 500);
        mGoogleApiClient = googleApiClient;
        mPlaceFilter = filter;
    }

    @Override
    public int getCount() {
        return mResultList.size();
    }

    @Override
    public PlaceAutocomplete getItem(int index) {
        return mResultList.get(index);
    }

    @Override
    public android.widget.Filter getFilter() {
        Filter filter = new Filter() {
            @Override
            public FilterResults performFiltering(CharSequence constraint) {
                FilterResults filterResults = new FilterResults();
                if (constraint != null && constraint.length() > 3 && constraint.length()%3 == 1) {
                    // Retrieve the autocomplete results.
                    mResultList = autocomplete(constraint.toString());

                    // Assign the data to the FilterResults
                    filterResults.values = mResultList;
                    filterResults.count = mResultList.size();
                }
                return filterResults;
            }

            @Override
            public void publishResults(CharSequence constraint, FilterResults results) {
                if (results != null && results.count > 0) {
                    notifyDataSetChanged();
                } else {
                    notifyDataSetInvalidated();
                }
            }
        };
        return filter;
    }


    public ArrayList<PlaceAutocomplete> autocomplete(String input) {

        if (mGoogleApiClient.isConnected()) {

            // Submit the query to the autocomplete API and retrieve a PendingResult that will
            // contain the results when the query completes.
            PendingResult results = Places.GeoDataApi.getAutocompletePredictions(mGoogleApiClient, input.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 = (AutocompletePredictionBuffer)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;
            }

            // 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;

    }

    class PlaceAutocomplete {
        public CharSequence placeId;
        public CharSequence description;

        PlaceAutocomplete(CharSequence placeId, CharSequence description) {
            this.placeId = placeId;
            this.description = description;
        }

        @Override
        public String toString() {
            return description.toString();
        }
    }
}

调用GeoDataApi.getAutocompletePredictions的行进入一个名为Filter.java,Log.java,handler.java和Looper.java的内部类,并在Looper.java的第121行无限循环(我是肯定的工作室sdk将显示Looper.java的代码。

它甚至没有抛出错误,或者转到下一行,它只是不起作用。另外,我无法看到错误的堆栈跟踪。

这是调用此代码的代码片段 -

if (mLastLocation != null) {
           GooglePlacesAutoCompleteAdapter placesAdapter = new GooglePlacesAutoCompleteAdapter(this, R.layout.item_list, mGoogleApiClient, mLastLocation, null);
            autoCompView.setAdapter(placesAdapter);
            autoCompView.setOnItemClickListener(this);
        }

有人可以告诉我这里我做错了什么吗?如有任何帮助,将不胜感激。我需要尽快让这个工作。

PS - 我在这里将mPlaceFilter传递为null。

1 个答案:

答案 0 :(得分:1)

在开发者控制台中启用适用于Android的Google Places API