Skip to content

Instantly share code, notes, and snippets.

@hleinone
Created February 6, 2019 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hleinone/b31e8ed6c3014fa76561378e9159910d to your computer and use it in GitHub Desktop.
Save hleinone/b31e8ed6c3014fa76561378e9159910d to your computer and use it in GitHub Desktop.
Google Maps SDK for Android Rx extension
import android.content.Context
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.SupportMapFragment
import io.reactivex.Observable
import io.reactivex.Single
import io.reactivex.disposables.Disposables
@androidx.annotation.CheckResult
fun SupportMapFragment.map(): Single<GoogleMap> =
Single.create { emitter ->
this.getMapAsync { googleMap ->
if (googleMap != null) {
emitter.onSuccess(googleMap)
} else {
emitter.onError(NullPointerException("Could not obtain GoogleMap"))
}
}
}
@androidx.annotation.CheckResult
fun GoogleMap.cameraMoveStarted(): Observable<Int> =
Observable.create { emitter ->
setOnCameraMoveStartedListener {
emitter.onNext(it)
}
}
@androidx.annotation.CheckResult
fun GoogleMap.cameraIdle(): Observable<Unit> =
Observable.create { emitter ->
setOnCameraIdleListener {
emitter.onNext(Unit)
}
}
@androidx.annotation.CheckResult
fun GoogleMap.myLocationButtonClicks(): Observable<Unit> =
Observable.create { emitter ->
setOnMyLocationButtonClickListener {
emitter.onNext(Unit)
false
}
emitter.setDisposable(Disposables.fromRunnable {
setOnMyLocationButtonClickListener(null)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment