If you want to create google map inside fragment than go threw this code
public class GoogleMapFragment extends Fragment implements OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{
private GoogleMap googleMap;
GoogleApiClient mGoogleApiClient;
Location mLastLocation;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_demo, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
mGoogleApiClient.connect();
}
@Override
public void onResume() {
super.onResume();
intiGoogleMap();
}
@Override
public void onStart() {
mGoogleApiClient.connect();
super.onStart();
}
@Override
public void onStop() {
mGoogleApiClient.disconnect();
super.onStop();
}
@Override
public void onDestroyView() {
super.onDestroyView();
SupportMapFragment supportMapFragment = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.mapMapAddress));
if (supportMapFragment != null)
getFragmentManager().beginTransaction().remove(supportMapFragment).commit();
}
private void intiGoogleMap() {
SupportMapFragment supportMapFragment = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.mapMapAddress));
supportMapFragment.getMapAsync(this);
}
@Override
public void onConnected(@Nullable Bundle bundle) {
//First check permissions if accept or reject
if (AppUtils.checkPermission(Manifest.permission.ACCESS_FINE_LOCATION, getActivity()) &&
AppUtils.checkPermission(Manifest.permission.ACCESS_COARSE_LOCATION, getActivity())) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (googleMap != null)
if (mLastLocation != null) {
//here you can place your marker
}
} else {
Toast.makeText(getActivity(), "Check your permission", Toast.LENGTH_LONG).show();
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
@Override
public void onMapReady(GoogleMap googleMap) {
if (googleMap != null) {
this.googleMap = googleMap;
this.googleMap.getUiSettings().setMyLocationButtonEnabled(true);
this.googleMap.getUiSettings().setCompassEnabled(true);
}
}
}
and fragment_demo.xml is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/mapMapAddress"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
and try to run this will be showing of google map, and make sure all permissions and you have created app in google developer console. Also make sure adding Android Key in manifest file.