vlio20
vlio20

Reputation: 9295

Map fragment in fragment

I have a fragment in which I have another fragment. Here is the outer fragment:

public class MapsFragment extends Fragment
{
   GoogleMap map;
   View rootView;

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

       map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map_frag_map_fragment)).getMap();
      .....
   }

}

Here is the map_frag layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <AutoCompleteTextView
        android:id="@+id/map_frag_location_AutoCompleteTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:ems="10" >

        <requestFocus />
    </AutoCompleteTextView>

    <fragment
        android:id="@+id/map_frag_map_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/map_frag_location_AutoCompleteTextView"
        android:layout_alignParentLeft="true"
        class="com.google.android.gms.maps.SupportMapFragment" />

</RelativeLayout>

I am getting this error: The method getSupportFragmentManager() is undefined for the type MapsFragment

I have seen many examples which works in Activities, but couldn't find any help with fragments.

Thanks

Upvotes: 1

Views: 1523

Answers (3)

Hannoun Yassir
Hannoun Yassir

Reputation: 21182

Your fragment should have type android.support.v4.app.Fragment and not android.app.Fragment

Upvotes: 1

Ravv
Ravv

Reputation: 482

    In Your Class
   --------------------------------------

  SupportMapFragment mSupportMapFragment;
  private GoogleMap googleMap;
  int ZOOM_LEVEL=15;

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
   View mTrackView = inflater
        .inflate(R.layout.mylayout, container, false);
    mSupportMapFragment = SupportMapFragment.newInstance();
    FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
    fragmentTransaction.add(R.id.mapwhere, mSupportMapFragment);
    fragmentTransaction.commit();

    return mTrackView;
}

  @Override
  public void onStart() {
    // TODO Auto-generated method stub
      super.onStart();

    if(mSupportMapFragment!=null){

        googleMap = mSupportMapFragment.getMap();
        if(googleMap!=null){
        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        googleMap.getUiSettings().setMyLocationButtonEnabled(false);
        googleMap.setMyLocationEnabled(false);


        CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(
                new LatLng(12.12122,
                    17.22323), ZOOM_LEVEL);
        googleMap.animateCamera(cameraUpdate);
          }
        }
  }

    mylayout.xml
   -----------------------------------------------
      <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >
 <FrameLayout
   android:layout_width="match_parent"
   android:layout_height="0dp"
   android:layout_weight="1.03"

   android:id="@+id/mapwhere" />


  <TextView
   android:layout_width="match_parent"
   android:layout_height="wrap_content"/>

</LinearLayout>

Upvotes: 0

seand
seand

Reputation: 478

Here's the code I use in my app:

import android.support.v4.app.Fragment;
...

public class MapFragment extends Fragment {

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // inflate layout

    GoogleMap map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
    ...
  }

}

Upvotes: 0

Related Questions