RxJava 库

RxJava 是一个反应式编程库,用于通过可观察序列编写异步程序和基于事件的程序。

借助 Places Rx 库,您可以在 Maps SDK for Android 和 Places SDK for Android 上接收异步事件的可观察序列,以便充分利用丰富的 RxJava 功能。

安装

如需在您的 Google 地图项目中安装 Places Rx 库,请执行以下操作:

  1. 将以下依赖项添加到您的模块级 build.gradle 文件:

    dependencies {
        // RxJava bindings for the Maps SDK
        implementation 'com.google.maps.android:maps-rx:1.0.0'
    
        // RxJava bindings for the Places SDK
        implementation 'com.google.maps.android:places-rx:1.0.0'
    
        // It is recommended to also include the latest Maps SDK, Places SDK and RxJava so you
        // have the latest features and bug fixes.
        implementation "com.google.android.gms:play-services-maps:18.2.0"
        implementation 'com.google.android.libraries.places:places:3.3.0'
        implementation 'io.reactivex.rxjava3:rxjava:3.1.8'
    
  2. 在 Android Studio 中重新构建您的项目,以同步这些更改。

用法示例

以下示例展示了如何在获取地点详情时接收 Single 并订阅它:

  placesClient.fetchPlace(
    placeId = "thePlaceId",
    placeFields = listOf(Place.Field.ID, Place.Field.NAME, Place.Field.ADDRESS),
    actions = {}
  ).subscribe(
    { response ->
      Log.d("PlacesRx", "Successfully got place ${response.place.id}")
    },
    { error ->
      Log.e("PlacesRx", "Could not get place: ${error.message}")
    }
  )
}

后续步骤