0

I am working on google map android , I want to use getSupportFragmentManager() in my fragment instead of activity but getSupportFragmentManager() not defined , what is the problem ?

This is my code

public class MapFragment extends Fragment implements OnMapReadyCallback {


   private GoogleMap mMap;





    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_map,
                container, false);




      //  MapFragment mapFragment = (MapFragment) getActivity().getFragmentManager().findFragmentById(R.id.map);

        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);





        return view;
    }


    @Override
    public void onMapReady(GoogleMap googleMap) {

        mMap = googleMap;
        // Add a marker in Sydney and move the camera
        LatLng TutorialsPoint = new LatLng(21, 57);
        mMap.addMarker(new
                MarkerOptions().position(TutorialsPoint).title("Tutorialspoint.com"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(TutorialsPoint));

    }
DeVYaGhI
  • 31
  • 7
  • 1
    Possible duplicate of [How can I access getSupportFragmentManager() in a fragment?](https://stackoverflow.com/questions/20237531/how-can-i-access-getsupportfragmentmanager-in-a-fragment) – Shmuel Jul 08 '18 at 11:32
  • use `getChildFragmentManager()` inside your fragment. – Alireza Ahmadi Jul 08 '18 at 14:16

1 Answers1

-1

EDITED: use this:

 // Remove old code
        // SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);

        // don't recreate fragment everytime ensure last map location/state are maintained
        if (mapFragment == null) {
            mapFragment = SupportMapFragment.newInstance();
            mapFragment.getMapAsync(new OnMapReadyCallback() {
                @Override
                public void onMapReady(GoogleMap googleMap) {
                    LatLng latLng = new LatLng(1.289545, 103.849972);
                    googleMap.addMarker(new MarkerOptions().position(latLng)
                            .title("Singapore"));
                    googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
                }
            });
        }

        // R.id.map is a FrameLayout, not a Fragment
        getChildFragmentManager().beginTransaction().replace(R.id.map, mapFragment).commit();

according to this link Add SupportMapFragment (Android Google Maps) In Fragment

  • I write the following SupportMapFragment mapFragment; mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map); mapFragment.getMapAsync(this); – DeVYaGhI Jul 08 '18 at 11:29
  • error: incompatible types: Fragment cannot be converted to SupportMapFragment – DeVYaGhI Jul 08 '18 at 11:31
  • @DeVYaGhI I edited my answer, hope it works this time. – fatemeh fallahi arezoudar Jul 08 '18 at 11:42
  • I have this error in the last line error: incompatible types: SupportMapFragment cannot be converted to Fragment – DeVYaGhI Jul 08 '18 at 12:58
  • @DeVYaGhI, you probably using two different versions of `Fragment` class. note that there is one in the Android SDK and one in the support library. Make sure (by checking your imports) both sides are the same (preferably from support library for backward compatibility) – Alireza Ahmadi Jul 08 '18 at 14:18