Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps Places Autocomplete Set Current Location Default Android

Is it possible that when I click on the autocomplete, it will set the current address as the default string? Pretty much like this post but I can't find any decent answer. Please help. Thank you! :)

like image 522
ayabbear Avatar asked Nov 08 '22 09:11

ayabbear


1 Answers

Despite it is already 3 years later, I decided to answer in case there is someone needs it. Since startActivityForResult is deprecated, I go for the new way to handle activity results here. When you are creating intent to open Autocomplete activity, you should apply setInitialQuery("Hawaii") before building it. Here is the whole code:

var resultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
    if (result.resultCode == Activity.RESULT_OK) {
        // There are no request codes
        val data: Intent? = result.data

        data?.let {
            val place = Autocomplete.getPlaceFromIntent(data)
            makeToast(place.name.toString()) // <- Consume your result
        }
    }
}

private fun openSearchAutocomplete() {
    val fields = listOf(Place.Field.ID, Place.Field.NAME, Place.Field.ADDRESS, Place.Field.LAT_LNG)

    val intent = Autocomplete.IntentBuilder(
        AutocompleteActivityMode.OVERLAY, fields).setCountry("US") //United States
        .setInitialQuery("Hawaii") // <- set initial query here
        .build(this)
    resultLauncher.launch(intent)
}
like image 153
Nodirxon Avatar answered Nov 10 '22 00:11

Nodirxon