I have a FragmentActivity and I want to use a map fragment within it. I'm having a problem getting the support fragment manager to access it.
if (googleMap == null) {
googleMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map1)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
// create marker
MarkerOptions marker = new MarkerOptions().position(
new LatLng(latitude, longitude)).title("Hello Maps ");
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(latitude, longitude)).zoom(15).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
// adding marker
googleMap.addMarker(marker);
if you have this problem and are on api level 21+ do this:
this will get the map when used inside of a fragment.
Simply get it like this -
OR
in your Fragment's onActivityCreated() method and any method that is being called after onActivityCreated().
The following code does the trick for me
You can simply access like
and then use
Kotlin users try this answer
do this in your fragment