Android LocationServices.FusedLocationApi deprecated

后端 未结 8 753
没有蜡笔的小新
没有蜡笔的小新 2020-12-02 05:39

I couldn\'t figure out why LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest, this); \"FusedLocationApi\" is cross out

相关标签:
8条回答
  • 2020-12-02 06:09

    Original Answer

    This is happening because FusedLocationProviderApi deprecated in a recent version of google play services. You can check it here. The official guide now suggests using FusedLocationProviderClient. You can find the detailed guide here.

    for e.g inside onCreate() or onViewCreated() create a FusedLocationProviderClient instance

    Kotlin

    val fusedLocationClient = LocationServices.getFusedLocationProviderClient(requireContext())
    

    and for requesting the last known location all you have to do is call

    fusedLocationClient.lastLocation.addOnSuccessListener { location: Location? ->
                location?.let { it: Location ->
                    // Logic to handle location object
                } ?: kotlin.run {
                    // Handle Null case or Request periodic location update https://developer.android.com/training/location/receive-location-updates
                }
            }
    

    Java

    FusedLocationProviderClient fusedLocationClient = LocationServices.getFusedLocationProviderClient(requireContext());
    

    and

    fusedLocationClient.getLastLocation().addOnSuccessListener(requireActivity(), location -> {
            if (location != null) {
                // Logic to handle location object
            } else {
                // Handle null case or Request periodic location update https://developer.android.com/training/location/receive-location-updates
            }
        });
    

    Simple, Isn't it?


    Important Update (October 24, 2017):

    Yesterday Google updated its official developer page with a warning that says

    Please continue using the FusedLocationProviderApi class and don't migrate to the FusedLocationProviderClient class until Google Play services version 12.0.0 is available, which is expected to ship in early 2018. Using the FusedLocationProviderClient before version 12.0.0 causes the client app to crash when Google Play services is updated on the device. We apologize for any inconvenience this may have caused.

    So I think we should continue using the deprecated LocationServices.FusedLocationApi until Google resolves the issue.


    Latest Update (November 21, 2017):

    The warning is gone now. Google Play services 11.6 November 6, 2017, release note says : I think Play Services won't crash when it updates itself in the background. So we can use new FusedLocationProviderClient now.

    0 讨论(0)
  • 2020-12-02 06:10

    problem is with your import statement

    remove this

    import android.location.LocationListener;
    

    add

    import com.google.android.gms.location.LocationListener;
    
    0 讨论(0)
提交回复
热议问题