Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kotlin 如何使用自定义类LocationCallback()获取片段中的LastLocation?_Kotlin_Location_Locationlistener - Fatal编程技术网

Kotlin 如何使用自定义类LocationCallback()获取片段中的LastLocation?

Kotlin 如何使用自定义类LocationCallback()获取片段中的LastLocation?,kotlin,location,locationlistener,Kotlin,Location,Locationlistener,我有两个片段(一个是地图,另一个是描述),都使用位置。 我在Kotlin上创建自定义类LocationRetriever,用于移动回调(单一责任原则)。 但现在我不明白如何使用LocationRetriever对象在onMapReady方法上获取最后一个位置 classLocationRetriever: class LocationRetriever ( private val mActivity: Context, private val mLocationListener:

我有两个片段(一个是地图,另一个是描述),都使用位置。 我在Kotlin上创建自定义类
LocationRetriever
,用于移动回调(单一责任原则)。 但现在我不明白如何使用LocationRetriever对象在
onMapReady
方法上获取最后一个位置

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"
}
}
在地图片段I上启动和停止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()
    }
实现方法OnLocation已从interface LocationListener更改,但不记录来自此方法的任何日志

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")
}

到底是什么问题?您是否尝试从
MapsFragment
的LocationListener接口方法记录函数?是否调用了它们中的任何一个?是的,我实现了interface LocationListener的一种方法