getMapAsync() in Fragment

后端 未结 4 1662
旧时难觅i
旧时难觅i 2020-12-05 05:19

I having trouble implementing Google Map in Fragment.

This is my the part of my fragment class:

public class FragmentStoreFinderMap extends Fragment          


        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-05 06:16

    After a few overheats in my head, thanks to this post and https://stackoverflow.com/a/34732804/3925554, I would say that currently there are two ways of adding a MapFragment to your app:

    
    

    By this way, you have to use a standard fragment and implement this inside. Actually you are inserting a fragment into another:

        @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    
        SupportMapFragment supportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
        supportMapFragment.getMapAsync(this);
    }
    

    The other way would be using the real SupportMapFragment and implement it in this manner:

        @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    
        getMapAsync(this);
    }
    

    And they work without having to set a layout in onCreateView neither call mapView.onCreate(savedInstanceState); and mapView.onResume(); manually inside onViewCreated(), what is not recommended anyway.

    Hope it helps!

提交回复
热议问题