mcharmas / Android-ReactiveLocation

Small library that wraps Google Play Service API in brilliant RxJava Observables reducing boilerplate to minimum.
2.11k stars 312 forks source link

How to get place name #194

Open tcqq opened 5 years ago

tcqq commented 5 years ago

@mcharmas Now I use the following code to get the place address, but how should I get the place name?

    private fun googleLocation() {
        locationProvider = ReactiveLocationProvider(applicationContext, ReactiveLocationProviderConfiguration
                .builder()
                .setRetryOnConnectionSuspended(true)
                .build())
        val locationRequest = LocationRequest.create()
                .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
                .setNumUpdates(1)
        locationProvider!!.getUpdatedLocation(locationRequest)
                .doOnNext {
                    latitude = it.latitude
                    longitude = it.longitude
                    Timber.d("latitude: $latitude longitude: $longitude")
                }
                .flatMap { location ->
                    locationProvider!!.getReverseGeocodeObservable(location.latitude, location.longitude, 1)
                }
                .map { addresses -> if (!addresses.isEmpty()) addresses[0] else null }
                .map(AddressToStringFunc())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .`as`(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(this, Lifecycle.Event.ON_DESTROY)))
                .subscribe(DisplayTextOnViewAction(location), ErrorHandler()).isDisposed
    }