RxJava は、観測可能なシーケンスを使って非同期かつイベントベースのプログラムを記述するための、リアクティブ プログラミング ライブラリです。
Places Rx ライブラリを使用すると、Maps SDK for Android と Places SDK for Android の非同期イベントの観測可能なシーケンスを取得して、RxJava の豊富な機能を活用できるようになります。
インストール
Google マップ プロジェクトに Places Rx ライブラリをインストールする手順は以下のとおりです。
モジュール レベルの
build.gradleファイルに次の依存関係を追加します。dependencies { // RxJava bindings for the Maps SDK implementation(libs.maps.rx) // RxJava bindings for the Places SDK implementation(libs.places.rx) // 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:20.0.0") implementation("com.google.android.libraries.places:places:5.3.0") implementation("io.reactivex.rxjava3:rxjava:3.1.12")
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}") } ) }
次のステップ
- Places Rx ライブラリ(GitHub プロジェクト ページ)を確認します。
- Places Rx ライブラリ API リファレンスをご覧ください。