Can't find the class com.google.android.gms.location.LocationClient (android)

后端 未结 11 1770
离开以前
离开以前 2020-12-15 16:03

I have download a demo project from http://developer.android.com/training/location/retrieve-current.html, and I think I don\'t lost any steps; But I can\'t find which jar fi

11条回答
  •  没有蜡笔的小新
    2020-12-15 16:45

    LocationClient is deprecated. You have to use GoogleApiclient, like this:

    1: Declare a GoogleApiClient variable

    private GoogleApiClient mGoogleApiClient;
    

    2: Instantiate

    mGoogleApiClient = new GoogleApiClient.Builder(mThisActivity)
         .addApi(LocationServices.API)
         .addConnectionCallbacks(this)
         .addOnConnectionFailedListener(this)
         .build();
    

    3: Implement Callback

    public class YourClass extends BaseFragment implements
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener, LocationListener {
    
        @Override
        public void onConnectionFailed(ConnectionResult result) {
            // your code goes here
        }
    
        @Override
        public void onConnected(Bundle connectionHint) {
            //your code goes here
        }
    
        @Override
        public void onConnectionSuspended(int cause) {
            //your code goes here       
        }
    }
    

    4: Start to get Location Updates:

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

    5: Remove Location Updates:

    LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
    

    6: Get Last Known Location:

    private Location mCurrentLocation;
    
    mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    

提交回复
热议问题