简体   繁体   中英

Android Google Maps V2: added marker doesn't appear when map is included as dynamic fragment

I need to add a map dynamically into the app to my MainActivity, otherwise I somehow suffer on memory leaks. Adding the map basically works, but I can't add markers. They just don't appear. That's how I add the map:

activity_main.xml which is used by the MainActivity

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- Placeholder for all fragments -->
    <RelativeLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

    </RelativeLayout>

    <!-- Slider menu -->
    <LinearLayout
        android:id="@+id/list_container"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#fff"
        android:orientation="vertical" >

    <!-- more code -->
    </LinearLayout>

</android.support.v4.widget.DrawerLayout>

fragment_map.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center" >

        <fragment
            xmlns:map="http://schemas.android.com/apk/res-auto"
            android:id="@+id/fragment_googlemap"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            class="com.androidmapsextensions.SupportMapFragment"
            map:cameraTargetLat="48.21167860510698"
            map:cameraTargetLng="16.365892895859423"
            map:cameraZoom="11"
            map:uiCompass="true"
            map:uiRotateGestures="true"
            map:uiScrollGestures="true"
            map:uiTiltGestures="true"
            map:uiZoomControls="true"
            map:uiZoomGestures="true" />
</FrameLayout>

MapFragment.java

public class MapFragment extends com.androidmapsextensions.SupportMapFragment
{
    private static final String TAG = MapFragment.class.getSimpleName();
    private OnFragmentLoadedCompleteListener onFragmentLoadedCompleteListener;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle args)
    {
        super.onCreateView(inflater, container, args);

        Logger.log(TAG, "onCreateView()", LogController.isLoggingEnabled(), Log.DEBUG);

        return inflater.inflate(R.layout.fragment_map, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);

        Logger.log(TAG, "onActivityCreated()", LogController.isLoggingEnabled(), Log.DEBUG);

        onFragmentLoadedCompleteListener.onFragmentComplete();
    }

    @Override
    public void onAttach(Activity activity)
    {
        super.onAttach(activity);

        Logger.log(TAG, "onAttach()", LogController.isLoggingEnabled(), Log.DEBUG);

        onFragmentLoadedCompleteListener = (OnFragmentLoadedCompleteListener)activity;
    }
}

Important parts of MainActivity.java

@Override
public void onFragmentComplete()
{
    googleMap = ((SupportMapFragment)getSupportFragmentManager().findFragmentByTag("mapFragment")).getExtendedMap();
}


private void initMapFragment()
{
    if (googleMap != null)
    {
        googleMap.clear();
        googleMap = null;
    }

    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new MapFragment(), "mapFragment").commit();
}

OnFragmentLoadedCompleteListener.java

public interface OnFragmentLoadedCompleteListener
{
    public void onFragmentComplete();
}

onFragmentComplete() is executed, googleMap is not null and shown. But I can't draw markers on it. They just don't appear.

But when I don't dynamically add the map and embed it into the activity_main.xml within the RelativeLayout like this

<RelativeLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <fragment
            xmlns:map="http://schemas.android.com/apk/res-auto"
            android:id="@+id/fragment_googlemap"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            class="com.androidmapsextensions.SupportMapFragment"
            map:cameraTargetLat="48.21167860510698"
            map:cameraTargetLng="16.365892895859423"
            map:cameraZoom="11"
            map:uiCompass="true"
            map:uiRotateGestures="true"
            map:uiScrollGestures="true"
            map:uiTiltGestures="true"
            map:uiZoomControls="true"
            map:uiZoomGestures="true" />
    </RelativeLayout>

and fetch the map like this without the listener, I can draw markers, that means they appear.

googleMap = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.fragment_googlemap)).getExtendedMap();

That means drawing markers on the map only works when I directly embed the map into the xml of the MainActivity instead of added it dynamically as fragment. What's going on here. Where is the error?

As is often the case I found the solution on my own.

I have to keep a reference to my MapFragment I use to replace:

mapFragment = new MapFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, mapFragment).commit();

In onFragmentComplete() I have to invoke getChildFragmentManager() on this reference to obtain the "correct" GoogleMap.

googleMap = ((SupportMapFragment)mapFragment.getChildFragmentManager().findFragmentById(R.id.fragment_googlemap)).getExtendedMap();

This time it can be found by id as well (didn't work before), and drawing markers on this map works now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM