Code example for LocationClient

Methods: disconnectisConnectedremoveLocationUpdates

1
    } 
 
    @Override 
    protected void onStop() { 
 
        if (mLocationClient.isConnected()) {
            stopLocationUpdates(); 
        } 
        mLocationClient.disconnect();
 
        super.onStop(); 
    } 
 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present. 
        getMenuInflater().inflate(R.menu.main, menu);
        return true; 
    } 
 
    @Override 
    public void onConnected(Bundle bundle) {
 
 
        Location location = mLocationClient.getLastLocation();
        if (location != null) {
            mBus.post(new LocationChangedEvent(new LatLng(location.getLatitude(), location.getLongitude()), "You are here!", true));
        } 
 
        startLocationUpdates(); 
 
    } 
 
    @Subscribe 
    public void OnMapClicked(MapClickedEvent event){
        stopLocationUpdates(); 
    } 
 
    @Override 
    public void onDisconnected() { 
        //Do something on disconnect 
    } 
 
    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) {
        //Do something if the connection to Google Play Services fails 
    } 
 
    @Override 
    public void onLocationChanged(Location location) {
        mBus.post(new LocationChangedEvent(new LatLng(location.getLatitude(), location.getLongitude()), "You are here!", true));
    } 
 
    private void startLocationUpdates() { 
        Log.d("FindMe", "Starting location updates...");
        mLocationClient.requestLocationUpdates(mLocationRequest, this);
    } 
 
    private void stopLocationUpdates() { 
        Log.d("FindMe", "Stopping location updates");
        mLocationClient.removeLocationUpdates(this);
    } 
}