diff --git a/demo-kotlin/app/build.gradle b/demo-kotlin/app/build.gradle index d7833934..fd697950 100644 --- a/demo-kotlin/app/build.gradle +++ b/demo-kotlin/app/build.gradle @@ -38,6 +38,7 @@ dependencies { // Places SDK for Android implementation 'com.google.android.libraries.places:places:3.0.0' + implementation 'com.google.maps.android:android-maps-utils:2.4.0' } repositories { mavenCentral() diff --git a/demo-kotlin/app/src/main/AndroidManifest.xml b/demo-kotlin/app/src/main/AndroidManifest.xml index 4e08eb4c..6066bb42 100644 --- a/demo-kotlin/app/src/main/AndroidManifest.xml +++ b/demo-kotlin/app/src/main/AndroidManifest.xml @@ -28,6 +28,14 @@ android:supportsRtl="true" android:theme="@style/Theme.AppCompat.Light"> + + + + @@ -38,6 +46,7 @@ + + view.setOnClickListener(null) + startAutocompleteIntent() + } + + // [START maps_solutions_android_autocomplete_define] + private val startAutocomplete = registerForActivityResult( + ActivityResultContracts.StartActivityForResult(), + ActivityResultCallback { result: ActivityResult -> + binding.autocompleteAddress1.setOnClickListener(startAutocompleteIntentListener) + if (result.resultCode == RESULT_OK) { + val intent = result.data + if (intent != null) { + val place = Autocomplete.getPlaceFromIntent(intent) + + // Write a method to read the address components from the Place + // and populate the form with the address components + Log.d(TAG, "Place: " + place.addressComponents) + fillInAddress(place) + } + } else if (result.resultCode == RESULT_CANCELED) { + // The user canceled the operation. + Log.i(TAG, "User canceled autocomplete") + } + } as ActivityResultCallback) + // [END maps_solutions_android_autocomplete_define] + + // [START maps_solutions_android_autocomplete_intent] + private fun startAutocompleteIntent() { + // Set the fields to specify which types of place data to + // return after the user has made a selection. + val fields = listOf( + Place.Field.ADDRESS_COMPONENTS, + Place.Field.LAT_LNG, Place.Field.VIEWPORT + ) + + // Build the autocomplete intent with field, country, and type filters applied + val intent = Autocomplete.IntentBuilder(AutocompleteActivityMode.OVERLAY, fields) + .setCountry("US") + //TODO: https://developers.google.com/maps/documentation/places/android-sdk/autocomplete + .setTypesFilter(listOf(TypeFilter.ADDRESS.toString().lowercase())) + .build(this) + startAutocomplete.launch(intent) + } + // [END maps_solutions_android_autocomplete_intent] + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + binding = AutocompleteAddressActivityBinding.inflate(layoutInflater) + val view = binding.root + setContentView(view) + + // Attach an Autocomplete intent to the Address 1 EditText field + binding.autocompleteAddress1.setOnClickListener(startAutocompleteIntentListener) + + // Update checkProximity when user checks the checkbox + val checkProximityBox = findViewById(R.id.checkbox_proximity) + checkProximityBox.setOnCheckedChangeListener { _: CompoundButton?, isChecked: Boolean -> + // Set the boolean to match user preference for when the Submit button is clicked + checkProximity = isChecked + } + + // Submit and optionally check proximity + val saveButton = findViewById