片段中的地图片段

时间:2013-11-25 18:06:05

标签: android google-maps android-fragments

我有一个片段,其中有另一个片段。 这是外部片段:

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();
      .....
   }

}

以下是map_frag布局:

<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>

我收到此错误: The method getSupportFragmentManager() is undefined for the type MapsFragment

我见过许多可以在“活动”中使用的示例,但是找不到任何有关碎片的帮助。

谢谢

3 个答案:

答案 0 :(得分:1)

您的片段应该有android.support.v4.app.Fragment类型,而不是android.app.Fragment

答案 1 :(得分:0)

以下是我在我的应用中使用的代码:

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();
    ...
  }

}

答案 2 :(得分:0)

    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>
相关问题