Skip to content Skip to sidebar Skip to footer

How To Add Map Fragment Programmatically

I would like add this xml fragment programmatically to other fragments. Is it possible? Copy

And then in code you can do:

FragmentManagerfm= getChildFragmentManager();
SupportMapFragmentsupportMapFragment=  SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.mapContainer, supportMapFragment).commit();

Solution 2:

Make a layout like:

<FrameLayoutandroid:id="@+id/layout_mapContainer"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_margin="5dp"android:layout_weight="0"android:background="@android:color/transparent"android:orientation="vertical" ><FrameLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:background="@android:color/transparent" /></FrameLayout>

In Activity declare :

FrameLayoutmapLayout= (FrameLayout)findViewById(R.id.layout_mapContainer);

initialise map like this:

privatevoidinitialiseMap() {

        FragmentTransaction mTransaction = getSupportFragmentManager().beginTransaction();
        SupportMapFragment mFRaFragment = new MapFragmentActivity();
        mTransaction.add(mapLayout.getId(), mFRaFragment);
        mTransaction.commit();

        try {
            MapsInitializer.initialize(context);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

Here MapFragmentActivity is class extends SupportMapFragment

Post a Comment for "How To Add Map Fragment Programmatically"