MapFragment in Fragment, alternatives?

后端 未结 8 608
猫巷女王i
猫巷女王i 2020-12-08 10:32

I need your help... I work on it until 3 days. My app is working with fragments. One of these fragments has to display a map from the Google Maps V2 api for Android.

相关标签:
8条回答
  • 2020-12-08 11:14

    Use SupportMapFragment to overcome this error:

    In fragment layout

    <fragment
    android:id="@+id/googleMap"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
    

    In Your Fragment onCreateView

    SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.googleMap);
    
            if (mapFragment != null) {
                mapFragment.getMapAsync(this);
            }
    

    Since your layout is in the Fragment's layout, therefore the SupportMapFragment is the child layout of your fragment. Hence use getChildFragmentManager() which is Fragment's FragmentManager

    0 讨论(0)
  • 2020-12-08 11:15

    Use MapView instead of MapFragment in your Fragment's layout. Remember to call MapView's lifecycle methods:

    • onCreate(Bundle)
    • onResume()
    • onPause()
    • onDestroy()
    • onSaveInstanceState(Bundle)
    • onLowMemory()

    as described here.

    Btw. you shouldn't be using MapFragment, only SupportMapFragment and support library.

    Edit:

    If you switch to support library, you can use code from comment #1 here: http://code.google.com/p/gmaps-api-issues/issues/detail?id=5064#c1

    0 讨论(0)
提交回复
热议问题