Search code examples
javaandroidgoogle-places-api

Unable to use two typeFilters in setTypeFilter() for Google Places 2019 update


I have upgraded to the new Google Places using:

'com.google.android.libraries.places:places:2.0.0'

I have implemented a AutoCompleteSupportFragment in my layout and in that fragment is where the user will type in their location or destination. Then the info will be put into the BottomSheetRiderFragment.

I want to be able to filter with Addresses and Establishments but I can't do both on separate lines for it to work.

From researching (not a whole lot on this topic) there was a mention of using

TYPE_FILTER_ADDRESS|TYPE_FILTER_ESTABLISHMENT

I tried putting:

places_location.setTypeFilter(TYPE_FILTER_ADDRESS|TYPE_FILTER_ESTABLISHMENT);

but this error occurred:

setType Filter(com.google.android.libraries.places.api.model.Type Filter) in AutocompleteSupportFragment cannot be applied to (int)

How can I fix this?

layout

<fragment
                android:id="@+id/places_location"
                android:layout_weight="5"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment" />

<fragment
                android:id="@+id/places_destination"
                android:layout_weight="5"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"/>

Activity

places_location.setOnPlaceSelectedListener(new PlaceSelectionListener() {
        @Override
        public void onPlaceSelected(@NonNull Place place) {
            // Get info about the selected place.
            Log.e(TAG, "Place: " + place.getName() + ", " + place.getId());

            mPlaceLocation = place.getAddress();
        }

        @Override
        public void onError(@NonNull Status status) {
            // Handle the error.
            Log.e(TAG, "An error occurred: " + status);
        }
    });


places_destination.setOnPlaceSelectedListener(new PlaceSelectionListener() {
        @Override
        public void onPlaceSelected(@NonNull Place place) {
            // Get info about the selected place.
            Log.e(TAG, "Place: " + place.getName() + ", " + place.getId());

            mPlaceDestination = place.getAddress(); // search bar destination
            Log.e(TAG, "mPlaceDestination = " + mPlaceDestination);

            // search_bar_destination ....
            search_bar_destination = mPlaceDestination;

            // Show information at bottom
            BottomSheetRiderFragment mBottomSheet = BottomSheetRiderFragment
                    .newInstance(mPlaceLocation, mPlaceDestination, false);
            RiderHome.super.onPostResume();
            mBottomSheet.show(getSupportFragmentManager(), mBottomSheet.getTag());
        }

        @Override
        public void onError(@NonNull Status status) {
            // Handle the error.
            Log.e(TAG, "An error occurred: " + status);
        }
    });

Also

// Create a RectangularBounds object.
RectangularBounds bounds = RectangularBounds.newInstance(
    new LatLng(45.282785, -66.235011),
    new LatLng(45.333059, -65.842197));

places_location.setCountry("ca");
places_location.setLocationBias(bounds);
places_location.setTypeFilter(TypeFilter.REGIONS);
places_location.setTypeFilter(TypeFilter.CITIES);
places_location.setTypeFilter(TYPE_FILTER_ADDRESS|TYPE_FILTER_ESTABLISHMENT);

Solution

  • You are not allowed to write more than one filter, it will not work if you write the two filters, If you want both establishment and address then dont write any filter, as both at the same time will not work. If you want to get suggestions for nearby location only, then Location Bias is perfect for you, it will give you both address and establishment within suggestions

    RectangularBounds bounds = RectangularBounds.newInstance
    (new LatLng(45.282785, -66.235011),new LatLng(45.333059, -65.842197));
    places_location.setCountry("ca");
    places_location.setLocationBias(bounds);