メインコンテンツまでスキップ

Place Autocomplete

This example does not contain a UI. Results will be logged in Android Studio.

PlaceAutocompleteKotlinExampleActivity.kt
package com.mapbox.search.sample.api

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.mapbox.search.autocomplete.PlaceAutocomplete
import com.mapbox.search.sample.R

class PlaceAutocompleteKotlinExampleActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val placeAutocomplete = PlaceAutocomplete.create(
accessToken = getString(R.string.mapbox_access_token),
)

lifecycleScope.launchWhenCreated {
val response = placeAutocomplete.suggestions(
query = "Washington DC",
)

if (response.isValue) {
val suggestions = requireNotNull(response.value)

Log.i("SearchApiExample", "Place Autocomplete suggestions: $suggestions")

if (suggestions.isNotEmpty()) {
// Supposing that a user has selected (clicked in UI) the first suggestion
val selectedSuggestion = suggestions.first()

Log.i("SearchApiExample", "Selecting first suggestion...")

val selectionResponse = placeAutocomplete.select(selectedSuggestion)
selectionResponse.onValue { result ->
Log.i("SearchApiExample", "Place Autocomplete result: $result")
}.onError { e ->
Log.i("SearchApiExample", "An error occurred during selection", e)
}
}
} else {
Log.i("SearchApiExample", "Place Autocomplete error", response.error)
}
}
}
}
この{Type}は役に立ちましたか?