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

次のステップ