I have an Activity of Map Fragments that I want to convert to Fragments

public class Trees extends FragmentActivity implements GoogleMap.OnMarkerClickListener {

    private GoogleMap mMap;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trees);
        setUpMapIfNeeded();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setUpMap();
    }

    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();
            mMap.setMyLocationEnabled(true);
            mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude),17));
        }
    }
}   

      

+3


source to share


1 answer


First of all, change FragmentActivity to Fragment. Also, since the MapFragment is also a fragment, you will now have nested fragments. There are many solutions / hacks available to use a nested fragment (for your case it is a MapFragment inside a fragment). But the most recommended is to add the MapFragment dynamically to the Fragemnt . To do this, you need to follow these simple steps.

First of all, you should just have a FrameLayout in your mapfragment.xml like this

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android
    android:id="@+id/fl_map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     >
</FrameLayout>

      

Now you need to dynamically add the SupportMapFragment to your fragment

private SupportMapFragment fragment;

      

Inside onActivityCreated () copy this code



@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    FragmentManager fm = getChildFragmentManager();

    if (fragment == null) {
        fragment = SupportMapFragment.newInstance();
        fm.beginTransaction().replace(R.id.fl_map, fragment).commit();
    }
}

      

since FragmentTransaction will take a while, so you need to initialize your map in onResume () like this

    @Override
public void onResume() {
    super.onResume();
if (mMap == null)
        mMap = fragment.getMap();
    try {
        if (mMap != null) {
            builder = new LatLngBounds.Builder();
            mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
            mMap.getUiSettings().setZoomControlsEnabled(false);
            mMap.getUiSettings().setZoomGesturesEnabled(true);
            mMap.setMyLocationEnabled(true);

            // do whatever you want to do with map
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

      

Also don't forget to add this code to onDetach () from snippet

@Override
public void onDetach() {
    super.onDetach();
    try {
        Field childFragmentManager = Fragment.class
                .getDeclaredField("mChildFragmentManager");
        childFragmentManager.setAccessible(true);
        childFragmentManager.set(this, null);

    } catch (NoSuchFieldException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    } catch (Exception e) {
        e.printStackTrace();
    }

}

      

Hope this helps you.

0


source







All Articles