-1

Google client implementation is given below:

   if (mGoogleApiClient == null) {
                mGoogleApiClient = new GoogleApiClient.Builder(this)
                        .addConnectionCallbacks((GoogleApiClient.ConnectionCallbacks) this)
                        .addOnConnectionFailedListener((GoogleApiClient.OnConnectionFailedListener) this)
                        .addApi(LocationServices.API)
                        .build();
            }

onStart i am connecting GoogleApiClient

  @Override
protected void onStart() {
    super.onStart();
    if (mGoogleApiClient != null)
        mGoogleApiClient.connect();
}

till here everything is fine but on connected method its return null

@Override
public void onConnected(Bundle bundle) {
    if (bundle != null)// always null
        Log.e(TAG, bundle.toString());
}

Getting code for location on onClick of floating button after connected to google api client

 //Floating Action Button
            floatingActionButton.setOnClickListener(new View.OnClickListener() {
                                                        @TargetApi(Build.VERSION_CODES.M)
                                                        @Override
                                                        public void onClick(View v) {
                                                            //To Check Runtime Permission
                                                            checkRuntimePermission();
                                                            mMap.clear();
                                                            mMap.setMyLocationEnabled(true);

                                                            mLocationRequest = new LocationRequest();
                                                            mLocationRequest.setInterval(50);
                                                            mLocationRequest.setFastestInterval(10);
                                                            mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);


                                                            mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

                                                            if (!Utils.GPS_SERVICE(mContext)) {
                                                                Utils.isGPSOnline(mContext);
                                                            }

                                                            if (mLastLocation != null) {
                                                                double lon = mLastLocation.getLongitude();
                                                                double lat = mLastLocation.getLatitude();
                                                                LatLng latlng = new LatLng(lat, lon);
                                                                mMap.addMarker(new MarkerOptions().position(latlng).title("My_location"));

                                                                //TO move the marker when user enter any Location
                                                                mMap.moveCamera(CameraUpdateFactory.newLatLng(latlng));
                                                                mMap.animateCamera(CameraUpdateFactory.zoomTo(15));
                                                            }
                                                        }
                                                    }
Nikhil Sharma
  • 593
  • 7
  • 23

3 Answers3

0

If onConnected() getting called, you GoogleApiClient is okay.

Why bundle is null?

Have a look at this method -

public abstract void onConnected (Bundle connectionHint)

Contents of the connectionHint Bundle are defined by the specific services. May be null if no content is provided by the service.

Paresh P.
  • 6,677
  • 1
  • 14
  • 26
0

Try

if (mGoogleApiClient == null) {
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addConnectionCallbacks((GoogleApiClient.ConnectionCallbacks) this)
                    .addOnConnectionFailedListener((GoogleApiClient.OnConnectionFailedListener) this)
                    .addApi(LocationServices.API)
.enableAutoManage(activityContext, this) // this - for implementing Interface
                    .build();
        }
Wilson Christian
  • 650
  • 1
  • 6
  • 17
0

Remove this condition as well if (mGoogleApiClient == null) and pass this in callbacks

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

You can Look this also for complete example http://www.androidhive.info/2015/02/android-location-api-using-google-play-services/

Quick learner
  • 10,632
  • 4
  • 45
  • 55