android-kotlin-LocationServices getFusedLocationProviderClient不起作用

问题描述 投票:0回答:2

我想在位置可用时返回当前用户位置。

这是我的代码:

fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
    locationCallback = object : LocationCallback() {
        override fun onLocationResult(locationResult: LocationResult) {
            super.onLocationResult(locationResult)
            if (locationResult.lastLocation != null && locationResult.lastLocation.latitude > 0 && locationResult.lastLocation.longitude > 0) {

                lastLocation = locationResult.lastLocation
                currentUserLatLng = LatLng(lastLocation.latitude, lastLocation.longitude)

                fusedLocationClient.removeLocationUpdates(locationCallback)

            }
        }
    }

问题是,它从未被调用过,我已经获得允许,并允许我的应用使用gps,我为其设置了调试器,但它永远不会出现

你能帮我吗?此代码有什么问题?

android android-location android-maps-v2
2个回答
0
投票

为什么要用代码编写locationResult.lastLocation.latitude > 0locationResult.lastLocation.longitude > 0?他们可以是负数


0
投票

您是否在某个地方打电话给requestLocationUpdates?

fusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.getMainLooper());
© www.soinside.com 2019 - 2024. All rights reserved.