Google Maps API v2 Custom MapFragment + SimpleFragment

Yuriy Aizenberg picture Yuriy Aizenberg · Jan 22, 2013 · Viewed 8.8k times · Source

I need to create a simple application which contain 1. Custom MapFragment which can display current location (LocationListener?), two markers, etc. 2. Simple empty Fragments.

The application must not use support v4 (sdk 11+) / FragmentActivity.

Empty fragment.

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/black" >
</RelativeLayout>

Class:

public class FirstFragment extends Fragment{

public FirstFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.first_fragment, container, false);
    return view;
}
}

Map fragment (try to make it singleton).

XML:

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/map"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  class="com.google.android.gms.maps.MapFragment"/>

Class

public class ThirdMapFragment extends MapFragment {
public static MapFragment instance;

@Override
public View onCreateView(LayoutInflater arg0, ViewGroup arg1, Bundle arg2) {
    View view;
    view = arg0.inflate(R.layout.map_fragment, arg1, false);
    return view;
}

public static MapFragment getInstance() {
    if (instance == null) {
        instance = newInstance();
    }
    // NullPointerException in getMap() ???
    // instance.getMap().addMarker(new MarkerOptions().position(new LatLng(41.1, 39)));
    return instance;
}

@Override
public void onCreate(Bundle arg0) {
    super.onCreate(arg0);
}
}

Main activity:

public class ActivityMain extends Activity implements OnClickListener {
FragmentManager fragmentManager;
FragmentTransaction transaction;
FirstFragment firstFragment;
MapFragment mapFragment;

// Buttons
Button b4;

int i = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    fragmentManager = getFragmentManager();
    transaction = fragmentManager.beginTransaction();

    b4 = (Button) findViewById(R.id.button4);

    b1.setOnClickListener(this);

    firstFragment = new FirstFragment();
    mapFragment =  ThirdMapFragment.getInstance();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
public void onClick(View v) {
    makeTransaction(v.getId());
}



private void makeTransaction(int id) {
    i++;
    transaction = fragmentManager.beginTransaction();
    if (i == 1) {
        transaction.replace(R.id.relativeLayout, firstFragment).commit();
    } else if (i == 2) {
        transaction.replace(R.id.relativeLayout, secondFragment).commit();
    } else {
    transaction.replace(R.id.relativeLayout, mapFragment).commit();
        i = 0;
    }
}
}

R.id.relativeLayout - Layout in main.xml

But I do not see the map (empty screen with zoom buttons) and how to make LocationListener? Thx.

Answer

Emil Adz picture Emil Adz · Jan 24, 2013

if you are using the support library you have to use the SupportMapFragment class and not the MapFragment for the map fragment. your activity should be a FragmentActivity for the support of the library. as well as for the Fragment manager. you have to getSupportFragmentManager().

Take a look at this code:

import com.google.android.gms.common.GooglePlayServicesUtil;
import android.os.Bundle;
import com.google.android.gms.maps.*;

public class Map extends android.support.v4.app.FragmentActivity
{
private GoogleMap map;


@Override
protected void onCreate(Bundle savedInstanceState) 
{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);

        if (GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext()) == 0)
        {
            map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
            map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        }
}}

If you still don't see a map after you made those changes. Check your API Console that you have turn on the Google Map ANDROID API V2 and not just the Google Map API V2 that could cause the same result as you describe.