0

I have a question and a problem. First of all I don't know is this possible:
I have a fragment activity with tabs. On one tab (which is a fragment) I have a map with a hidden list of items. Map is putted in a frame of that fragment. The list is downloaded and on a button press is visible again. On a map I have markers that represents that items.
My problem is this: I always get map to be null when a use getMap() from a fragment that I put in a frame.
Is this possible? And if not what do you recommend? Thanks in advance.


EDITED

public class MapExplore extends Fragment{
private FrameLayout frame;
    private ListView list;

    private String api_key;

    private GoogleMap map;
    private MapExploreAdapter adapter;

    private SupportMapFragment map_fragment;
@Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        api_key = Configuration.get_prefereence_string(getActivity(), "user_activation_key", null);

        adapter = new MapExploreAdapter();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.map_explore, null);
        frame = (FrameLayout) view.findViewById(R.id.frameMap);

        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        setUpMapFragment();
    }

    private void setUpMapFragment(){
        if(map_fragment == null){
            map_fragment = SupportMapFragment.newInstance();
            FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
            ft.add(frame.getId(), map_fragment, Utils.MAP_FRAGMENT_TAG);
            ft.commit();

        }
    }

    private void setUpMap(){
        Log.i("SET UP MAP", "Started");
        if(map == null){
            map = ((SupportMapFragment) map_fragment).getMap();
        }
        if(map != null){
            map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(Utils.SF_LAT, Utils.SF_LON), 13));
        }       
    }
@Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        setUpMap();
    }

I edited my question with some code...

Jovan
  • 1,741
  • 4
  • 19
  • 38

1 Answers1

3

If you could post some of you code it would be easier to diagnose your problem:

meantime try creating your SupportMap fragment dynamically as follows:

mMapFragment = new SupportMapFragment() {
            @Override
            public void onActivityCreated(Bundle savedInstanceState) {
                super.onActivityCreated(savedInstanceState);
                GoogleMap map = mMapFragment.getMap();
                if (map != null) {
                    //Your initialization code goes here
                }
            }
        };

Altenative approach to use Mapview inside fragment as follows:

public class Yourfragment extends Fragment {

    private MapView mMapView;
    private GoogleMap mMap;
    private Bundle mBundle;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View inflatedView = inflater.inflate(R.layout.map_fragment, container, false);

        try {
            MapsInitializer.initialize(getActivity());
        } catch (GooglePlayServicesNotAvailableException e) {
            // TODO handle this situation
        }

        mMapView = (MapView) inflatedView.findViewById(R.id.map);
        mMapView.onCreate(mBundle);
        setUpMapIfNeeded(inflatedView);

        return inflatedView;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBundle = savedInstanceState;
    }

    private void setUpMapIfNeeded(View inflatedView) {
        if (mMap == null) {
            mMap = ((MapView) inflatedView.findViewById(R.id.map)).getMap();
            if (mMap != null) {
                setUpMap();
            }
        }
    }

    private void setUpMap() {
        mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
    }

    @Override
    public void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        mMapView.onPause();
    }

    @Override
    public void onDestroy() {
        mMapView.onDestroy();
        super.onDestroy();
    }
}

And put map view in XML as follows:

 <com.google.android.gms.maps.MapView
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

Here is a link to the official documentation for MapView

rene
  • 41,474
  • 78
  • 114
  • 152
Karan_Rana
  • 2,813
  • 2
  • 26
  • 35