Initialize MapFragment programmatically with Maps API v2

The good AnderWebs gave me an answer in Google+ but he is too laz…. emm busy to write it here again, so here is the short version:
Extend the MapFragment class and override the onCreateView() method. After this method is done we can get a non-null reference to que GoogleMap object.

This is my particular solution:

public class MiniMapFragment extends SupportMapFragment {
    private LatLng mPosFija;

    public MiniMapFragment() {

    public static MiniMapFragment newInstance(LatLng posicion){
        MiniMapFragment frag = new MiniMapFragment();
        frag.mPosFija = posicion;
        return frag;

    public View onCreateView(LayoutInflater arg0, ViewGroup arg1, Bundle arg2) {
        View v = super.onCreateView(arg0, arg1, arg2);
        return v;

    private void initMap(){
        UiSettings settings = getMap().getUiSettings();

        getMap().addMarker(new MarkerOptions().position(mPosFija).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)));

Now in the previous Fragment class I do

mMapFragment = MiniMapFragment.newInstance(new LatLng(37.4005502611301, -5.98233461380005));

Maybe it’s not perfect yet, because the screen blinks when showing the map. But not sure if the problem is because of this or something else.

Leave a Comment