我有一个地图片段活动,我想将其转换为片段

时间:2014-11-24 03:52:29

标签: android

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

1 个答案:

答案 0 :(得分:0)

首先,将FragmentActivity更改为Fragment。此外,由于MapFragment也是片段,因此您现在将拥有嵌套片段。有很多解决方案/黑客可以使用嵌套的Fragment(对于你的情况,它是Fragment中的MapFragment)。但最值得推荐的是动态地将MapFragment添加到您的Fragemnt 。为此,你必须遵循这些简单的步骤。

首先,您应该在mapfragment.xml中使用FrameLayout,就像这样

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

现在您必须在Fragment中动态添加SupportMapFragment

private SupportMapFragment fragment;

onActivityCreated()中,复制此代码

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

因为FragmentTransaction需要一些时间,所以你必须在 onResume()中初始化你的地图

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

也不要忘记在Fragment的 onDetach()中添加此代码

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

}

希望这会对你有所帮助。