本文介绍了Android LocationServices.FusedLocationApi已弃用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚为什么 LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest,this); FusedLocationApi交叉指出并指出它已被弃用。
我认为Play Services在后台自动更新时不会崩溃。所以我们现在可以使用新的 FusedLocationProviderClient






重要更新(2017年10月24日):



昨天Google通过
所以我我们认为我们应该继续使用已弃用的 LocationServices.FusedLocationApi ,直到Google解决问题为止。






原始答案



发生这种情况是因为 FusedLocationProviderApi 在最新版本的Google Play服务中已弃用。您可以查看。官方指南现在建议使用。您可以在找到详细指南。



例如内部 onCreate()创建 FusedLocationProviderClient 实例

  mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this); 

并且要申请最后一个已知位置,您只需致电

  mFusedLocationClient.getLastLocation()
.addOnSuccessListener(this,new OnSuccessListener< Location>(){
@Override
public void onSuccess(位置位置){
//得到上一个已知位置。在极少数情况下,这可以为null。
if(location!= null){
//处理位置的逻辑对象
}
}
});

简单不是吗?


I couldn't figure out why LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest, this); "FusedLocationApi" is cross out and point at it saying is deprecated.Click here to view Image

import android.location.Location;
import android.location.LocationListener;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MaintainerMapActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener{

private GoogleMap mMap;
GoogleApiClient mGoogleApiClient;
Location mLastLocaton;
LocationRequest mLocationRequest;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maintainer_map2);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}


@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    // Add a marker in Sydney and move the camera
    LatLng sydney = new LatLng(-34, 151);
    mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
    mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}

@Override
public void onLocationChanged(Location location) {

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}

@Override
public void onConnected(@Nullable Bundle bundle) {
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(1000);
    mLocationRequest.setFastestInterval(1000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,mLocationRequest, this);
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}

}

解决方案

Latest Update (November 21, 2017):

The warning is gone now. I think Play Services won't crash when it updates itself in the background. So we can use new FusedLocationProviderClient now.


Important Update (October 24, 2017):

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

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


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() create a FusedLocationProviderClient instance

 mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);

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

mFusedLocationClient.getLastLocation()
    .addOnSuccessListener(this, new OnSuccessListener<Location>() {
        @Override
        public void onSuccess(Location location) {
            // Got last known location. In some rare situations, this can be null.
            if (location != null) {
                // Logic to handle location object
            }
        }
    });

Simple isn't it?

这篇关于Android LocationServices.FusedLocationApi已弃用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-04 08:43