Getting a Reference to a GoogleMap in a fragment using Support Library v4

拟墨画扇 提交于 2019-12-11 02:53:46

问题


I'm trying to add Google Maps to a fragment using the v4 support lib, but I have some trouble using getMap() to reference it.

I'm adding the fragment like so (following the example at http://developer.android.com/training/basics/fragments/fragment-ui.html#AddAtRuntime) :

public class MainActivity extends FragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        if (findViewById(R.id.fragment_container) != null) {

            if (savedInstanceState != null) {
                return;
            }

            MapFragmentClass mapFragmentClass= new MapFragmentClass ();
            mapFragmentClass.setArguments(getIntent().getExtras());
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment_container, mapFragmentClass).commit();
        }
    }
}

And in the mapFragmentClass I'm trying to get the reference like this:

public class MapFragmentClass extends Fragment {

    public static  GoogleMap mMap;
    private static SupportMapFragment mapFragment;

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

        SupportMapFragment mapFragment = SupportMapFragment.newInstance();
        mMap = mapFragment.getMap();

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

}

or like this

@Override
public void  onResume(){
    super.onResume();
    mMap = mapFragment.getMap();

    Log.i("TestMap", "setCamera");
}

Both options come back with a null.. So basically I don't have a reference to the map when the Fragment is active. I even also tried getting a reference 10 seconds after the map was loaded, but alas..

Why won't it return a GoogleMap? The var mapFragment is always filled, from the moment I instantiate it in onCreateView, so if that's filled, I should be able to get a GoogleMap type back right then & there, correct?

I've tried loading in a GoogleMap a normal Activity, using a Fragement in the layout, and there, in onCreate, I can instantiate and reference it, no problem. The map seems to load in faster as well.

If anybody has any idea how to get this working, I'd be much obliged.

Edit: My mapfragment layout:

<?xml version="1.0" encoding="utf-8"?>

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.google.android.gms.maps.MapFragment"
          tools:layout="@layout/main"/>

回答1:


In your FragmentTransaction in MainActivity you should add an instance of SupportMapFragment instead of MapFragmentClass.

public class MainActivity extends FragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        if (findViewById(R.id.fragment_container) != null) {

            SupportMapFragment mapFragmentClass= new SupportMapFragment ();
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment_container, mapFragmentClass).commit();
            getSupportFragmentManager().executePendingTransactions();
            GoogleMap map = mapFragmentClass.getMap();
            // do what you need to do with the map
        }
    }
}

The way you were creating SupportMapFragment was just creating a new Fragment instance but since you didn't add it using a FragmentTransaction none of the lifecycle methods of the Fragment were being called.



来源:https://stackoverflow.com/questions/22677989/getting-a-reference-to-a-googlemap-in-a-fragment-using-support-library-v4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!