ไลบรารี RxJava

RxJava เป็นไลบรารีการเขียนโปรแกรมเชิงรับสำหรับการเขียนโปรแกรมแบบอะซิงโครนัสและอิงตามเหตุการณ์โดยใช้ลำดับที่สังเกตได้

ไลบรารี Maps Rx ช่วยให้คุณรับลำดับที่สังเกตได้สำหรับเหตุการณ์แบบอะซิงโครนัสใน Maps SDK สำหรับ Android และ Places SDK สำหรับ Android เพื่อให้คุณใช้ประโยชน์จากชุดฟีเจอร์ RxJava ที่หลากหลายได้

การติดตั้ง

วิธีติดตั้งไลบรารี Maps Rx ในโปรเจ็กต์ Google Maps

  1. เพิ่มทรัพยากร Dependency ต่อไปนี้ลงในไฟล์ 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")

  2. สร้างโปรเจ็กต์ใหม่ใน Android Studio เพื่อซิงค์การเปลี่ยนแปลงเหล่านี้

ตัวอย่างการใช้

รับ Observable สำหรับกิจกรรมการคลิกเครื่องหมายเป็นฟังก์ชันส่วนขยายในออบเจ็กต์ GoogleMap ดังนี้

googleMap.markerClickEvents()
    .subscribe { marker ->
        Log.d("MapsRx", "Marker ${marker.title} was clicked")
    }

ใช้โอเปอเรเตอร์ merge RxJava เพื่อรวมเหตุการณ์ต่างๆ ของกล้องไว้ในสตรีม Observable เดียว

Observable.merge(
    googleMap.cameraIdleEvents(),
    googleMap.cameraMoveEvents(),
    googleMap.cameraMoveCanceledEvents(),
    googleMap.cameraMoveStartedEvents(),
).subscribe {
    // Notified when any camera event occurs
}

ขั้นตอนถัดไป