我尝试使用Place与jetpack,但我找不到任何资源,如何实现这一点。
@Composable
fun PlaceSearchView() {
// Initialize the SDK
Places.initialize(applicationContext, "apiKey")
val placesClient = Places.createClient(this)
}上面的代码是基于文档中可用的内容,我得到的是错误。
未解决的参考资料: applicationContext
我现在的问题是:是否有专门的方法在Jetpack撰写中使用google?
在此上下文中未定义“此”
发布于 2022-05-10 13:11:44
您可以使用此代码创建启动自动完成小部件的意图。代码类似于这
@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 =
"")
}
}https://stackoverflow.com/questions/70683569
复制相似问题