-1

I wish to insert a Android map v2 into a fragment, but my restiction is that I can't use .xml files. The problem appears when I try to instantiate the MapFragment. Here is the code:

public class LocationFragment extends Fragment{

    private static View view;
    private static GoogleMap mMap;

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

        if (container == null) {

            return null;
        }
        view = (RelativeLayout) inflater.inflate(R.layout.location_fragment, container, false);

        setUpMapIfNeeded(); // For setting up the MapFragment

        MapFragment mf = new MapFragment();

        return view;
    }

    /***** Sets up the map if it is possible to do so *****/
    public static void setUpMapIfNeeded() {

        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {

            FragmentManager manager = MapActivity.fragmentManager;
            MapFragment smf = (MapFragment) manager.findFragmentById(R.id.location_map);
            mMap = smf.getMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null)
                setUpMap();
        }
    }

Can somebody help me?

Luciano Rodríguez
  • 2,239
  • 3
  • 19
  • 32
Viherbar
  • 239
  • 3
  • 13

2 Answers2

1

If you are using fragment what I suggest it's the following:

public class MapFragment extends Fragment {

     private static GoogleMap mMap;
     private static UiSettings mUiSettings;

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

          if (mMap == null)
             mMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.mapfragment)).getMap();

          mMap.setMyLocationEnabled(true);
          mUiSettings = mMap.getUiSettings();
          mUiSettings.setZoomControlsEnabled(false);
          mUiSettings.setCompassEnabled(true);
          mUiSettings.setMyLocationButtonEnabled(true);


         return rootView;
     }
}

The code from PagerAdapter to call this fragment could be like this:

class AppSectionsPagerAdapter extends FragmentStatePagerAdapter {

    public AppSectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public int getCount() {
        return NUM_PAGES;
    }

    @Override
    public Fragment getItem(int i) {
        switch (i) {
            case 0:
                // Map Fragment Activity
                return new MapFragment();

            default:
                return null;
        }
    }
}

Hope this helps!

Alberto
  • 426
  • 6
  • 16
0

You should really be using getMapAsync(). But you can do something like this, using fragment transactions to add a map fragment programatically.

MapFragment smf = MapFragment.newInstance(); manager.beginTransaction() .add(FRAGMENT_CONTAINER_ID, smf) .commit();

Joe Birch
  • 371
  • 2
  • 7