How get LastLocation in Fragment using custom class LocationCallback()?

176 Views Asked by At

I have two Fragment (one - map, other - description), both use location. I create custom class LocationRetrieveron Kotlin from this tutorial for moving the callbacks (Single-Responsibility Principle). But now I don't understand how can get last location on onMapReady method using object of LocationRetriever.

class LocationRetriever:

class LocationRetriever (
    private val mActivity: Context,
    private val mLocationListener: LocationListener?) : LocationCallback() {
private var mLocationClient: FusedLocationProviderClient? = null

private fun getLocationAvailability() {
    try {
        mLocationClient!!.locationAvailability.addOnCompleteListener { task -> onLocationAvailability(task.result!!) }
    } catch (ex: IllegalStateException) {
        Log.w(LOG_TAG, ex.toString())
    } catch (ex: SecurityException) {
        Log.w(LOG_TAG, ex.toString())
    }
}

override fun onLocationAvailability(availability: LocationAvailability) {
    if (availability.isLocationAvailable) {
        requestLocationUpdates()
    }
}

override fun onLocationResult(result: LocationResult) {
    if (mLocationListener != null) {
        val location = result.lastLocation
        mLocationListener.onLocationChanged(location)
    }
}

@Throws(SecurityException::class)
private fun requestLocationUpdates() {
    val req = LocationRequest.create()
    req.interval = LOCATION_UPDATE_INTERVAL
    req.fastestInterval = LOCATION_UPDATE_INTERVAL
    req.smallestDisplacement = 50f
    req.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
    mLocationClient!!.requestLocationUpdates(req, this, null)
}

fun hasPermissions(context: Context, vararg permissions: String): Boolean = permissions.all {
    ActivityCompat.checkSelfPermission(context, it) == PackageManager.PERMISSION_GRANTED
}

fun start() {
    val permission = Manifest.permission.ACCESS_FINE_LOCATION
    if (hasPermissions(mActivity, permission)) {
        mLocationClient = LocationServices.getFusedLocationProviderClient(mActivity)
        getLocationAvailability()
    }
}

fun stop() {
    if (mLocationClient != null) {
        mLocationClient!!.removeLocationUpdates(this)
    }
}

companion object {
    private const val LOCATION_UPDATE_INTERVAL: Long = 2000
    private const val LOG_TAG = "LocationRetriever"
}
}

On Map Fragment I start and stop LocationRetriever

    class MapsFragment : BaseExampleFragment(), ... LocationListener, ...{
    .....
    private var mLocationListener
        = object: LocationListener {
    fun onLocationChanged(location: Location) {}
    fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) {}
    fun onProviderEnabled(provider: String?) {}
    fun onProviderDisabled(provider: String?) {}
}
....
    override fun onStart() {
        super.onStart()
        LocationRetriever(context!!, mLocationListener).start()
    }

    override fun onStop() {
        super.onStop()
        LocationRetriever(context!!, mLocationListener).stop()
    }

Implementation method onLocationChanged from interface LocationListener, but doesn`t log anything Log from this method.

override fun onLocationChanged(p0: Location?) {
    val latitude = p0?.latitude
    val longitude = p0?.longitude
    val msg = "New Latitude: " + latitude + " New Longitude: " + longitude
    Log.i(TAG, "!!!onLocationChanged!!! onLocationChanged(): $msg")
}
0

There are 0 best solutions below