Skip to content

googlemaps/android-places-compose

Maven Central Version Static Badge GitHub License

Android Places Compose Library (Alpha)

Description

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.

Requirements

  • 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.

Installation

Add the dependency below to your module-level Gradle build file:

Kotlin (build.gradle.kts)

dependencies {
    implementation("com.google.maps.android:places-compose:0.1.0")
}

Groovy (build.gradle)

dependencies {
    implementation 'com.google.maps.android:places-compose:0.1.0'
}

Sample App

This repository includes a full-featured demo app that demonstrates how to use the library. To run the demo app, follow these steps:

  1. Clone the repository
  2. Get a Places API key
  3. Copy local.defaults.properties to a new file in the same folder secrets.properties and replace DEFAULT_API_KEY with your API key(s). (Note: this file should NOT be under version control to protect your API key)
  4. Build and run

Usage

Place Autocomplete

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.

Landmark Selection

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.

Contributing

Contributions are welcome and encouraged! See contributing for more info.

Terms of Service

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.

Support

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.

About

No description, website, or topics provided.

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages