Skip to content

A lightweight and ready to use API to search places for Compose Multiplatform

License

Notifications You must be signed in to change notification settings

ngallazzi/KMP-Places-Autocomplete

Repository files navigation

KMP Places Autocomplete 📌

Kotlin Compose Multiplatform badge-android badge-ios

Introduction

A simple Compose Multiplatform library to fill addresses, cities and countries in a form, based on Google Places API by Google https://developers.google.com/maps/documentation/places/web-service/autocomplete. For Android and IOS

Screenshots

City - Android Country - Android Address - Android

City - IOS Country - IOS Address - IOS

⚙️ Setup

Please generate a valid Google Places API key and add it to your project. You can find the instructions to generate key on Google docs: https://developers.google.com/maps/documentation/places/web-service/get-api-key Put your inside your project local.properties file:

api_key=<YOUR_API_KEY>

Then add the dependency to your project:

repositories {
    mavenCentral()
}

dependencies {
    implementation("io.github.ngallazzi:KMP-Places-Autocomplete:<LAST_VERSION>")
}

Usage

Just put PlaceAutoCompleteTextField composable within your app theme. Supported places are: City, Country, Address Want to use

@Composable
@Preview
fun App() {
    MaterialTheme {
        // Basic usage
        PlaceAutoCompleteTextField(
            label = "Please enter your city",
            type = City::class,
            onSuggestionSelected = {
                // DO SOMETHING
            }
        )
    }
}
@Composable
@Preview
fun App() {
    MaterialTheme {
        // Extended mode usage 
        PlaceAutoCompleteTextField(
            label = "Please enter your city",
            type = Address::class,
            isExtendedModeActive = true,
            onSuggestionSelected = {
                // DO SOMETHING
            }
        )
    }
}

// Complete parameters list
@Composable
fun PlaceAutoCompleteTextField(
    modifier: Modifier = Modifier,
    label: String = "",
    text: String = "",
    type: KClass<out Place>,
    keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
    keyboardActions: KeyboardActions = KeyboardActions.Default,
    singleLine: Boolean = false,
    maxLines: Int = if (singleLine) 1 else Int.MAX_VALUE,
    minLines: Int = 1,
    isExtendedModeActive: Boolean = false,
    enabled: Boolean = true,
    readOnly: Boolean = false,
    leadingIcon: @Composable (() -> Unit)? = null,
    trailingIcon: @Composable (() -> Unit)? = null,
    supportingText: @Composable (() -> Unit)? = null,
    languageCode: String = Locale.current.language,
    textStyle: TextStyle = LocalTextStyle.current,
    colors: TextFieldColors = OutlinedTextFieldDefaults.colors(),
    shape: Shape = OutlinedTextFieldDefaults.shape,
    onSuggestionSelected: (String) -> Unit = {},
)

💬 Feedback

Please feel free to open an issue if you have any feedback or suggestions. PRs are welcome too!

©️ Credits

KMP Places Autocomplete is brought to you by these contributors.

📜 License

This project is licensed under the GNU GENERAL PUBLIC LICENSE - see the LICENSE.md file for details