ไลบรารี 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 '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.4.0'
        implementation 'io.reactivex.rxjava3:rxjava:3.1.8'
    
  2. สร้างโปรเจ็กต์อีกครั้งใน Android Studio เพื่อซิงค์การเปลี่ยนแปลงเหล่านี้

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

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

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

ตัวอย่างถัดไปจะแสดงวิธีใช้โอเปอเรเตอร์ RxJava ซึ่งก็คือ merge เพื่อรวมเหตุการณ์ต่างๆ จากกล้องไว้ในสตรีมที่สังเกตได้รายการเดียว

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

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