FusedLocationApi的问题,它已经被废弃了。
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了FusedLocationApi的问题,它已经被废弃了。相关的知识,希望对你有一定的参考价值。
我做了一个google地图的应用,但是当我接受权限的时候,它就关闭了。所有的代码都没问题,但onConnected方法。
这里是。
@Override
public void onConnected(@Nullable Bundle bundle) { //La aplicacion busca la ubicación dentro de este metodo
myLocationRequest = new LocationRequest();
myLocationRequest.setInterval(1000); //La información de ubicación se recoge en itnervalos de 1000 milisegundos para no agotar demasiado rapido la bateria
myLocationRequest.setFastestInterval(1000);
myLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); //setPriority se utiliza para controlar el uso de la bateria
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
LocationServices.FusedLocationApi.requestLocationUpdates(myGoogleApiClient, myLocationRequest, this); //Busca la ubicación de locationrEquest
}
}
我猜是FusedLocationApi.requestLocationUpdates出了问题 因为它说那里有一个错误 而且文字是删除线。更准确的说,它说'FusedLocationapi'已经被废弃了,我不知道这是什么意思。它建议将第3个参数改为'com.google.android.gms.location.LocationListener',但如果我这样做,应用程序就不能正常工作。如果我接受这个权限,它就会突然关闭。
我把整个代码留在这里,以防你需要检查什么。
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
private GoogleMap mMap;
Location myLastLocation;
LocationRequest myLocationRequest;
GoogleApiClient myGoogleApiClient;
Marker myCurrLocationMarker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
checkLocationPermission();
}
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
}
}
else {
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
}
}
protected synchronized void buildGoogleApiClient(){
myGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
myGoogleApiClient.connect();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
myLocationRequest = new LocationRequest();
myLocationRequest.setInterval(1000);
myLocationRequest.setFastestInterval(1000);
myLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
LocationServices.FusedLocationApi.requestLocationUpdates(myGoogleApiClient, myLocationRequest, this);
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onLocationChanged(Location location) {
myLastLocation = location;
if (myCurrLocationMarker != null) {
myCurrLocationMarker.remove();
}
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title("My Position");
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
myCurrLocationMarker = mMap.addMarker(markerOptions);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(11));
if (myGoogleApiClient != null) {
LocationServices.FusedLocationApi.removeLocationUpdates(myGoogleApiClient, this);
}
}
@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 onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;
public boolean checkLocationPermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.ACCESS_FINE_LOCATION)) {
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSIONS_REQUEST_LOCATION);
}
else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, MY_PERMISSIONS_REQUEST_LOCATION);
}
return false;
}
else {
return true;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_LOCATION: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
if (myGoogleApiClient == null) {
buildGoogleApiClient();
}
mMap.setMyLocationEnabled(true);
}
}
else {
Toast.makeText(this, "Permission not given.", Toast.LENGTH_LONG).show();
}
return;
}
}
}
}
谢谢你,如果你有什么想法,请告诉我。
答案
我认为是与导入的东西,尝试添加com.google.android.gms.location.LocationListener,像这样的MapsActivity。
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, com.google.android.gms.location.LocationListener {
//
}
以上是关于FusedLocationApi的问题,它已经被废弃了。的主要内容,如果未能解决你的问题,请参考以下文章
setMockMode 不起作用 - FusedLocationApi
FusedLocationApi 性能问题:精度似乎上限为 10.0 米