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 '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'
- 在 Android Studio 中重新建立專案,以同步處理這些變更。
使用範例
以下範例說明如何在擷取 Place Details 時,接收 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}") } ) }
後續步驟
- 查看 GitHub 的 Places Rx 程式庫專案頁面。
- 查看 Places Rx 程式庫 API 參考資料。