Jetpack 与 Google Place API 组合

问题描述 投票:0回答:2

我正在尝试将 Place SDK 与 jetpack compose 结合使用,但我找不到任何有关如何实现此功能的资源。

@Composable
fun PlaceSearchView() {
    // Initialize the SDK
    Places.initialize(applicationContext, "apiKey")

    
    val placesClient = Places.createClient(this)
}

上述代码基于文档中提供的内容,但我收到错误

未解决的参考:applicationContext

我现在的问题:是否有专门的方法在 Jetpack Compose 中使用 google place API?

“this”在此上下文中未定义

google-places-api android-jetpack-compose
2个回答
3
投票

您可以使用此代码创建一个意图来启动自动完成小部件作为意图。代码类似于this

@HiltViewModel
class MyViewModel @Inject constructor(private val myUseCase: MyUseCase, @ApplicationContext applicationContext: Context): ViewModel() {
    init {
        Places.initialize(applicationContext, "insert api key")
    }

    val field = listOf(Place.Field.NAME, Place.Field.LAT_LNG)

    }
@Composable
fun MyScreen(myViewModel: MyViewModel = hiltViewModel()) {
    val context = LocalContext.current
    val intent = Autocomplete.IntentBuilder(AutocompleteActivityMode.OVERLAY, exploreViewModel.field).build(context)

    val launcher = rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) {
       
        if(it.resultCode == RESULT_OK){
            val place = Autocomplete.getPlaceFromIntent(it.data    
            latLng = place.latLng
            Log.d("place LatLng: ", latLng.toString())
             
            // move the camera position of the map

          // cameraPositionState.move(CameraUpdateFactory.newLatLngZoom(lonLat, 15f))
        }

    }
     FloatingActionButton(modifier = Modifier
            .align(Alignment.TopStart)
            .padding(10.dp),
            onClick = {
                launcher.launch(intent)
            }) {
            Icon(imageVector = Icons.Default.Search, contentDescription = 
      "")
        }
}

0
投票

您始终可以将片段集成到 Jetpack Compose 中。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/address_auto_complete_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:theme="@style/ThemeOverlay.DeliveryAppDemo.FullscreenContainer"
    tools:context=".composable.AddressAutoCompleteFragment">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/autocomplete_fragment"
        android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</FrameLayout>
@Composable
fun AutoCompleteFragmentScreen() {
    Log.i(TAG, "AutoCompleteFragmentScreen context: ${LocalContext.current}")
    val activity = LocalContext.current as? MainActivity ?: return

    Log.i(TAG, "AutoCompleteFragmentScreen has activity: $activity")

    AndroidViewBinding(FragmentAddressAutoCompleteBinding::inflate) {

        val autoCompleteFragment =
            activity.supportFragmentManager.findFragmentById(R.id.autocomplete_fragment) as AutocompleteSupportFragment?

        autoCompleteFragment?.setPlaceFields(listOf(Place.Field.ID, Place.Field.NAME))

        // Set up a PlaceSelectionListener to handle the response.
        autoCompleteFragment?.setOnPlaceSelectedListener(object : PlaceSelectionListener {
            override fun onPlaceSelected(place: Place) {
                // TODO: Get info about the selected place.
                Log.i(TAG, "Place: ${place.name}, ${place.id}")
            }

            override fun onError(status: Status) {
                // TODO: Handle the error.
                Log.i(TAG, "An error occurred: $status")
            }
        })
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.