This is a Jetpack Compose library for the Google Maps Platform Places SDK for Android. It provides a reusable Place Autocomplete composable based on the new Places API.
Additionally, there is a sample app showing how to use the widget as well as demonstrating Address Descriptors and address entry.
- Android API 24 (or newer) (Android 7.0 Nougat)
- Android Studio (Koala or newer recommended)
- A Google Maps Platform API key from a project with the Places API (New) enabled.
Add the dependency below to your module-level Gradle build file:
dependencies {
implementation("com.google.maps.android:places-compose:0.1.2")
}
dependencies {
implementation 'com.google.maps.android:places-compose:0.1.2'
}
This repository includes a full-featured demo app that demonstrates how to use the library. To run the demo app, follow these steps:
- Clone the repository
- Get a Places API key
- Copy
local.defaults.properties
to a new file in the same foldersecrets.properties
and replaceDEFAULT_API_KEY
with your API key(s). (Note: this file should NOT be under version control to protect your API key) - Build and run
The PlacesAutocompleteTextField
composable provides a text field for place autocomplete. As the
user types, the composable will show a list of
Places Autocomplete suggestions
that the user can select from.
See PlacesAutocompleteMinimalActivity for a very minimal working example.
class PlacesAutocompleteMinimalActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Places.initializeWithNewPlacesApiEnabled(this, BuildConfig.PLACES_API_KEY)
val placesClient = Places.createClient(this)
val bias: LocationBias = RectangularBounds.newInstance(
LatLng(39.9, -105.5), // SW lat, lng
LatLng(40.1, -105.0) // NE lat, lng
)
setContent {
val searchTextFlow = MutableStateFlow("")
val searchText by searchTextFlow.collectAsStateWithLifecycle()
var predictions by remember { mutableStateOf(emptyList<AutocompletePrediction>()) }
LaunchedEffect(Unit) {
searchTextFlow.debounce(500.milliseconds).collect { query : String ->
val response = placesClient.awaitFindAutocompletePredictions {
locationBias = bias
typesFilter = listOf(PlaceTypes.ESTABLISHMENT)
this.query = query
countries = listOf("US")
}
predictions = response.autocompletePredictions
}
}
AndroidPlacesComposeDemoTheme {
Scaffold(
topBar = {
TopAppBar(
title = { Text("Places Autocomplete") }
)
}
) { paddingValues: PaddingValues ->
PlacesAutocompleteTextField(
modifier = Modifier.fillMaxSize().padding(paddingValues),
searchText = searchText,
predictions = predictions.map { it.toPlaceDetails() },
onQueryChanged = { searchTextFlow.value = it },
onSelected = { autocompletePlace : AutocompletePlace ->
// Handle the selected place
Toast.makeText(
this@PlacesAutocompleteMinimalActivity,
"Selected: ${autocompletePlace.primaryText}",
Toast.LENGTH_SHORT
).show()
},
)
}
}
}
}
}
NOTE: This sample is for demonstration purposes. Please follow Android best practices when building production ready apps.
The demo code shows how to use address descriptors to assist in locating an address. Note that address descriptors are not available in all locations.
Contributions are welcome and encouraged! See contributing for more info.
This library uses Google Maps Platform services. Use of Google Maps Platform services through this library is subject to the Google Maps Platform Terms of Service.
This library is not a Google Maps Platform Core Service. Therefore, the Google Maps Platform Terms of Service (e.g. Technical Support Services, Service Level Agreements, and Deprecation Policy) do not apply to the code in this library.
This library is offered via an open source license. It is not governed by the Google Maps Platform Support Technical Support Services Guidelines, the SLA, or the Deprecation Policy (however, any Google Maps Platform services used by the library remain subject to the Google Maps Platform Terms of Service).
This library adheres to semantic versioning to indicate when backwards-incompatible changes are introduced. Accordingly, while the library is in version 0.x, backwards-incompatible changes may be introduced at any time.
If you find a bug or have a feature request, please file an issue. Or, if you'd like to contribute, send us a [pull request] and refer to our code of conduct and contributing guide.
You can also reach us on our Discord channel.