从Observable获取结果 – Kotin Android

我有一个方法返回Observable,如下所示:

fun currentLocation(): Observable<LatLngFix> { if (GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(context) != ConnectionResult.SUCCESS || !context.isLocationPermissionGranted()) { return getDefaultLocation() } // lastKnownLocation returns location or complete without result // in the latter case, return default unknown location return Observable .concat(locationProvider.lastKnownLocation.map { val latLng = LatLngFix(it.latitude, it.longitude) return@map latLng }, getDefaultLocation()) .onErrorResumeNext { getDefaultLocation() } .first() } 

然后当我调用这个函数,我想获得latlng变量。 所以我需要订阅的Observable oviously,但不知道为什么,下面给出了一个错误:

 localisationService.currentLocation().subscribe(latLng -> { }) 

说latLng没有定义。 什么是检索价值的正确方法?

应该

 localisationService.currentLocation().subscribe { latLng -> } 

(注意大括号内的latLng参数)

要么

 localisationService.currentLocation().subscribe ({ latLng -> }, { error -> }) 

如果你还想实现onError