FusedLocationApi 的问题,已弃用

Posted

技术标签:

【中文标题】FusedLocationApi 的问题,已弃用【英文标题】:Problem with FusedLocationApi, it is deprecated 【发布时间】:2020-08-01 19:25:27 【问题描述】:

我制作了一个谷歌地图应用程序,但是当我接受权限时它会关闭。除了 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”已被弃用,我不知道这是什么意思。任何想法? 它建议将第三个参数更改为“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;
            
        
    

谢谢你,如果你有任何想法请告诉我。

【问题讨论】:

使用FusedLocationProviderClient 它没有用,任何其他想法@IntelliJ Amiya 【参考方案1】:

我认为与导入有关,尝试将 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 的问题,已弃用的主要内容,如果未能解决你的问题,请参考以下文章

Python 3 pyplot.hold 已弃用

如何解决“VisibleDeprecationWarning:zmq.eventloop.minitornado 已弃用”?

现在 FusedLocationApi 已被弃用,当我也有位置监听器时,如何实现 FusedLocationProvideClient?

代码分析/FxCop CA1726:UsePreferredTerms 显示已取消为已弃用

PHP 已弃用:each() 函数已弃用 [重复]

Modernizr.load 已弃用。 Yepnope.js 已弃用。怎么办?