Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/dkhawk/autocomplete widget places 3.3.0 #245

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@
android:theme="@style/AppTheme">
<activity
android:name=".DemoActivity"
android:exported="true"
android:label="@string/app_name">
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ class DemoActivity : AppCompatActivity() {
.inflate(R.layout.item_demo, this)
}
}
}
}
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ plugins {
id "org.jetbrains.dokka" version "1.9.10"
}


ext.projectArtifactId = { project ->
if (project.name == 'places-ktx') {
return project.name
Expand Down
5 changes: 4 additions & 1 deletion local.defaults.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# This file contains a default value for your GMP API Key.
# To provide your actual GMP API Key, update the local.properties file using the PLACES_API_KEY
# as demonstrated below.
PLACES_API_KEY="YOUR_API_KEY"
PLACES_API_KEY=DEFAULT_API_KEY

# Used by the new client demo
MAPS_API_KEY=DEFAULT_API_KEY
1 change: 1 addition & 0 deletions new-places-client/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
92 changes: 92 additions & 0 deletions new-places-client/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
id("com.google.android.libraries.mapsplatform.secrets-gradle-plugin")
}

android {
namespace = "com.example.new_places_client"
compileSdk = 34

defaultConfig {
applicationId = "com.example.new_places_client"
minSdk = 26
targetSdk = 34
versionCode = 1
versionName = "1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary = true
}
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
buildFeatures {
compose = true
buildConfig = true
}
composeOptions {
kotlinCompilerExtensionVersion = "1.5.9"
}
packaging {
resources {
excludes += "/META-INF/{AL2.0,LGPL2.1}"
}
}
}

dependencies {
implementation("androidx.core:core-ktx:1.12.0")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.7.0")
implementation("androidx.activity:activity-compose:1.8.2")
implementation(platform("androidx.compose:compose-bom:2024.01.00"))
implementation("androidx.compose.ui:ui")
implementation("androidx.compose.ui:ui-graphics")
implementation("androidx.compose.ui:ui-tooling-preview")
implementation("androidx.compose.material3:material3-android:1.2.1")

testImplementation("junit:junit:4.13.2")
testImplementation("com.google.truth:truth:1.4.2")

androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
androidTestImplementation(platform("androidx.compose:compose-bom:2024.01.00"))
androidTestImplementation("androidx.compose.ui:ui-test-junit4")

debugImplementation("androidx.compose.ui:ui-tooling")
debugImplementation("androidx.compose.ui:ui-test-manifest")

implementation("com.google.android.libraries.places:places:3.3.0")
implementation("com.google.android.gms:play-services-maps:18.2.0")
implementation(project(":places-ktx"))

// Jetpack navigation
val navVersion = "2.7.7"
// Jetpack Compose Integration
implementation("androidx.navigation:navigation-compose:$navVersion")

// Only needed for Google Maps compose
implementation("com.google.maps.android:maps-compose:4.3.3")
}

secrets {
// To add your Maps API key to this project:
// 1. Create a file ./secrets.properties in the root folder of the project
// 2. Add this line, where YOUR_API_KEY is your API key:
// PLACES_API_KEY=YOUR_API_KEY
propertiesFileName = "secrets.properties"
defaultPropertiesFileName = "local.defaults.properties"
}
21 changes: 21 additions & 0 deletions new-places-client/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.example.new_places_client

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.example.new_places_client", appContext.packageName)
}
}
44 changes: 44 additions & 0 deletions new-places-client/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="utf-8"?><!--
Copyright 2024 Google LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<application
android:name=".NewPlacesApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AndroidPlacesKtx">

<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="${MAPS_API_KEY}" />

<activity
android:name=".MainActivity"
android:exported="true"
android:theme="@style/Theme.AndroidPlacesKtx">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.example.new_places_client

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Card
import androidx.compose.material3.OutlinedCard
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import com.google.android.libraries.places.ktx.widget.ExperimentalPlacesApi
import com.google.android.libraries.places.ktx.widget.PlacesAutocomplete
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.model.CameraPosition
import com.google.android.gms.maps.model.LatLng
import com.google.android.libraries.places.api.model.AutocompletePrediction
import com.google.android.libraries.places.api.model.Place
import com.google.android.libraries.places.api.model.PlaceTypes
import com.google.android.libraries.places.api.model.RectangularBounds
import com.google.android.libraries.places.api.net.PlacesClient
import com.google.android.libraries.places.ktx.api.net.awaitFetchPlace
import com.google.maps.android.compose.GoogleMap
import com.google.maps.android.compose.Marker
import com.google.maps.android.compose.MarkerState
import com.google.maps.android.compose.rememberCameraPositionState
import kotlinx.coroutines.ExperimentalCoroutinesApi

private val predictionsHighlightStyle = SpanStyle(fontWeight = FontWeight.Bold, color = Color.Blue)

@OptIn(ExperimentalCoroutinesApi::class, ExperimentalPlacesApi::class)
@Composable
fun AutocompleteScreen(placesClient: PlacesClient, onShowMessage: (String) -> Unit) {
// The list of place details fields to retrieve from the server for the selected place.
// See the full list at https://developers.google.com/maps/documentation/places/android-sdk/place-data-fields
val fields = listOf(Place.Field.ID, Place.Field.NAME, Place.Field.ADDRESS, Place.Field.LAT_LNG)

var selectedPlace by remember {
mutableStateOf<AutocompletePrediction?>(null)
}

var placeDetails by remember {
mutableStateOf<PlaceDetails?>(null)
}

LaunchedEffect(selectedPlace) {
placeDetails = selectedPlace?.placeId?.let { placeId ->
placesClient.awaitFetchPlace(placeId, fields).place.toPlaceDetails()
}
}

val resources = LocalContext.current.resources

Column(Modifier.fillMaxSize()) {
PlacesAutocomplete(
placesClient,
searchLabelContent = { Text(stringResource(id = R.string.auto_complete_hint)) },
actions = {
locationBias = RectangularBounds.newInstance(
LatLng(39.95106, -105.31828), // SW lat, lng
LatLng(40.07399, -105.18096) // NE lat, lng
)
typesFilter = listOf(PlaceTypes.ESTABLISHMENT)
countries = listOf("US")
},
onPlaceSelected = { place ->
place?.getPrimaryText(null)?.toString()?.let { placeText ->
onShowMessage(resources.getString(R.string.selected_place, placeText))
}
selectedPlace = place
},
modifier = Modifier.fillMaxWidth(),
predictionsHighlightStyle = predictionsHighlightStyle
)

selectedPlace?.let { prediction ->
Spacer(modifier = Modifier.height(16.dp))
Card(
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 8.dp)
) {
Text(
modifier = Modifier.padding(8.dp),
text = prediction.getFullText(null).toString()
)
}
}

placeDetails?.let { place ->
Spacer(modifier = Modifier.height(8.dp))
val cameraPositionState = rememberCameraPositionState {
position = CameraPosition.fromLatLngZoom(place.location, 15f)
}

LaunchedEffect(place.location) {
cameraPositionState.animate(
CameraUpdateFactory.newLatLngZoom(place.location, 15f)
)
}

OutlinedCard(
modifier = Modifier
.fillMaxWidth()
.padding(8.dp)
) {
GoogleMap(
modifier = Modifier.fillMaxSize(),
cameraPositionState = cameraPositionState
) {
Marker(
state = MarkerState(place.location),
title = place.name,
snippet = place.address
)
}
}
}
}
}

Loading
Loading