Created
February 6, 2019 09:15
-
-
Save hleinone/b31e8ed6c3014fa76561378e9159910d to your computer and use it in GitHub Desktop.
Google Maps SDK for Android Rx extension
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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