2017-09-27 32 views
-1

我有可观察到的返回如下的方法:从可观测获取结果 - Kotin的Android的

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) 
       [email protected] latLng 
      }, getDefaultLocation()) 
      .onErrorResumeNext { getDefaultLocation() } 
      .first() 
} 

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

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

}) 

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

+0

这不是kotlin lambda函数语法。 – Christopher

回答

0

应该

localisationService.currentLocation().subscribe { latLng -> 

} 

(注意花括号内的经纬度参数)

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

}, 
{ error -> 
}) 

,如果你也想实现onError