RxJava ライブラリ

RxJava は、観測可能なシーケンスを使って非同期かつイベントベースのプログラムを記述するための、リアクティブ プログラミング ライブラリです。

Places Rx ライブラリ を使用すると、Maps SDK for Android で非同期イベントの監視可能なシーケンスを受け取ることができます。 と Places SDK for Android を実装することで、RxJava の豊富な機能セットを活用できます。

インストール

Places Rx ライブラリを Google マップ プロジェクトにインストールする手順は次のとおりです。

  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:19.0.0")
        implementation("com.google.android.libraries.places:places:3.5.0")
        implementation("io.reactivex.rxjava3:rxjava:3.1.8")
    
  2. Android Studio でプロジェクトを再ビルドして、これらの変更を同期します。

サンプル使用量

次の例は、Pub/Sub メッセージを受け取る 独身 Place Details を取得する際にサブスクライブします。

  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}")
    }
  )
}

次のステップ