Kotlin AutoComplete Google Places (KAGP)

In Android development, implementing worldwide place search becomes easier and more enjoyable with the Google Places SDK for Android which provides place autocomplete service.

The autocomplete service in the Places SDK for Android displays place predictions in response to a user's search query. As the user types, the autocomplete service returns suggestions for places such as businesses, addresses, and points of interest.

In this post, we will explore different ways to add autocomplete search features to Android apps. They are:

  • Add as an autocomplete widget to save development time and ensure a consistent user experience.
  • Get programmatic location predictions to create customized user experiences.

NOTE: You will need to create an API Key from the google cloud platform. You will also need to register your app in the Google API Console to get an API KEY. Once you have your API key, you are good to go… make sure to keep it safe.

Add the following dependencies to your project's build.gradle and sync your app:

implementation ' com.google.android.libraries.places:places: 1.1 . 0 '

Copy the API key from the Google console and add it to your strings.xml folder like this:

<string name= "api_key" >replace me</string>

In the activity you want to implement the autocomplete search feature for, you need to initialize a Place like this:

String apiKey = getString(R.string.api_key);

        /** 
         * Initialize Places. For simplicity, the API key is hard-coded. In a production 
         * environment we recommend using a secure mechanism to manage API keys. 
         */ 
        if (!Places.isInitialized()) {
            Places.initialize(getApplicationContext(), apiKey);
        }
        
// Create a new Places client instance. 
PlacesClient placesClient = Places.createClient( this );

Now we are ready to add the autocomplete search feature!

Embed an AutocompleteSupportFragment in your layout.

<fragment android:id= "@+id/autocomplete_fragment" 
  android:layout_width= "match_parent" 
  android:layout_height= "wrap_content" 
  android:name= "com.google.android.libraries.places.widget.AutocompleteSupportFragment"
  />

Next, initialize AutocompleteSupportFragment and set the fields to determine which data types to return in the activity or fragment.

// Initialize the AutocompleteSupportFragment.
        AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment)
                getSupportFragmentManager().findFragmentById(R.id.autocomplete_fragment);

        autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME));

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

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

See the results

Filter Type

If you look at the code below, I only set one filter, even though it could be done more than one.

var autoCompleteRequest = FindAutocompletePredictionsRequest.builder().setLocationBias(
            bounds).setCountry( "TR" ).setQuery(query).setSessionToken(
            AutocompleteSessionToken.newInstance()).setTypeFilter(ADDRESS).build()

Here's how

.setTypeFilter(TYPE_FILTER_CITIES|TYPE_FILTER_ADDRESS)

Post a Comment

Previous Next

نموذج الاتصال