FusedLocationClient不调用onLocationResult

时间:2018-10-01 08:39:28

标签: android fusedlocationproviderclient

此刻我正在创建位置客户端,

 public void startUpdatingLocationProcess(Context context) {

     parentContext = context;

    if (mFusedLocationClient == null){
        mFusedLocationClient = LocationServices.getFusedLocationProviderClient(parentContext);

        LocationRequest mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(5000);
        mLocationRequest.setFastestInterval(5000);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

        if (ContextCompat.checkSelfPermission(parentContext, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
        {
            mFusedLocationClient.requestLocationUpdates(mLocationRequest, new LocationCallback(){
                @Override
                public void onLocationResult(LocationResult locationResult) {

                    onLocationChanged(locationResult.getLastLocation());
                }

            } , Looper.myLooper());

         }


}

这是我要在整个应用程序生命周期中运行的LocationApi类中,此特定应用程序运行许多活动,因此我不想在每次销毁并创建新活动时“重新创建”请求。

允许并检查FINE_LOCATION的权限,logcat中没有错误,并且代码在创建mFusedLocationClient对象时运行requestLocationUpdates方法,但从未调用过“ onLocationResult”方法。

使用此api是否缺少我的东西?

3 个答案:

答案 0 :(得分:2)

在请求位置更新之前,您的应用必须连接到位置服务并发出位置请求。 像这样的东西:

private static LocationRequest createLocationRequest() {
    LogHelper.trace("createLocationRequest");
    LocationRequest mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(200000);
    mLocationRequest.setFastestInterval(300000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    return mLocationRequest;
}

public static void checkLocationService(final Fragment fragment, final FusedLocationProviderClient client, final OnSuccessListener<LocationSettingsResponse> successListener, OnFailureListener failureListener) {

    LogHelper.trace("checkLocationService");
    final LocationRequest request = createLocationRequest();
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
            .addLocationRequest(request);

    SettingsClient settingsClient = LocationServices.getSettingsClient(fragment.getActivity());
    Task<LocationSettingsResponse> task = settingsClient.checkLocationSettings(builder.build());

    task.addOnSuccessListener(fragment.getActivity(), new OnSuccessListener<LocationSettingsResponse>() {
        @Override
        public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
            LogHelper.trace("onSuccess");
            startLocationService(client, request, new LocationCallback());
            successListener.onSuccess(locationSettingsResponse);
        }
    });

    task.addOnFailureListener(fragment.getActivity(), failureListener);
}

答案 1 :(得分:1)

尝试通过android设置激活/停用位置。

答案 2 :(得分:0)

location == null

另见LocationAvailability No Location Available。如果你打电话

fusedLocationClient.getLocationAvailability()
    .addOnSuccessListener(this, locationAvailability -> {
    })

您会看到 LocationAvailability[isLocationAvailable: false]

可能是因为旧的模拟器或

locationRequest = LocationRequest.create();
locationRequest
        .setNumUpdates(1)
        .setFastestInterval(0)
        .setSmallestDisplacement(0)

如果你设置了setNumUpdates(1),它只会返回一次坐标,可能会有null。我删除了这些行并使用

@SuppressLint("MissingPermission")
private fun startLocationUpdates() {
    fusedLocationProviderClient?.requestLocationUpdates(locationRequest, locationCallback, Looper.getMainLooper())
}

开始搜索位置,然后

locationCallback = object : LocationCallback() {
    override fun onLocationResult(locationResult: LocationResult?) {
        super.onLocationResult(locationResult)

        if (locationResult?.lastLocation == null) {
            Timber.d("Location missing in callback.")
        } else {
            Timber.d(
                "Location Callback ${locationResult.lastLocation}")
            latitude = locationResult.lastLocation.latitude
            longitude = locationResult.lastLocation.longitude
            stopLocationUpdates()
        }
    }
}

fusedLocationProviderClient?.lastLocation
    ?.addOnSuccessListener { location ->
        Timber.d("lastLocation success $location")
        if (location == null) {
            startLocationUpdates()
        } else {
            latitude = location.latitude
            longitude = location.longitude
        }
    }
    ?.addOnFailureListener { failure ->
        Timber.d("lastLocation failure ${failure.message}")
    }

接收坐标。