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));
}
}
}