Android MapFragment Extension

I have an activity that contains two fragments. One fragment provides some information to the user in various fields. In another fragment, the Map should be displayed.

For the fragment on which the map is placed, I need to add custom logic to display markers and several other things that I would like to use elsewhere in the application. What is the best way to create a map without defining a fragment in xml, for example:

<fragment
class="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

Thanks Nathan

Edit:

Now I'm using nested fragments, not sure if this is the best way to handle this? Now my map fragment:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.MapFragment" />
</LinearLayout>

and code fragments:

public class ViewCurrentMapFragment extends Fragment implements View.OnClickListener {
    public static final String TAG = "MAP_FRAGMENT";

    private GoogleMap map;


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

    private void getCurrentMarkers() {
        // Getting reference to the SupportMapFragment of activity_main.xml
        SupportMapFragment smf = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map));

        map = smf.getMap();

    }

    @Override
    public void onClick(View v) {
    }


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

}

Now the problem is that smf.getMap () returns null. Any ideas?

Edit2:

To work by changing:

com.google.android.gms.maps.MapFragment

to

com.google.android.gms.maps.SupportMapFragment
+4
1

:

<fragment
    class="com.mycompany.MyCustomFragmentExtendingMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

, : http://code.google.com/p/gmaps-api-issues/issues/detail?id=5064#c1

+1

All Articles