0

Am trying to create and app with one activity but multiple fragment. i am using android.support.v4.* to build the fragment. Each item i have created so far by extending Fragment, I want to use the google Map Api2 in my application,

All the tutoril i found so far adding the "SupportMapFragment" as an element to layout file.

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

But i cannot use this way because nested fragment not supporting, how can i use the google map api2 as a fragment in my application

use the mapview as suggested in Comments

 public class InteractiveMapsWidget extends Fragment {
        private MapView mMapView;
        private GoogleMap mMap;

     public void onCreate(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            Log.i(TAG, "ON CREATED");
            Activity activity = getActivity();
            if (activity != null) {
                Bundle bundle = getArguments();
    `
            }
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)     {
            Log.i(TAG, "onCreateView");
            new DataWorkerAsyncTask(getActivity()).execute(widgetId);
            mMapView = (MapView) view.findViewById(R.id.map);
            mMapView.onCreate(savedInstanceState);
            setUpMapIfNeeded();

            return view;
        }


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

            setUpMapIfNeeded();
        }

        private void setUpMapIfNeeded() {
            if (mMap == null) {
                mMap = ((MapView) view.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 onPause() {
            mMapView.onPause();
            super.onPause();
        }

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

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

        @Override
        public void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
        }
    }

Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
  <LinearLayout
        android:id="@+id/root_container"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
        <com.google.android.gms.maps.MapView
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>

</LinearLayout>
Sam
  • 6,215
  • 9
  • 71
  • 90
  • Have a look at the samples that are bundled with the new maps api. In particlar, you may be interested in `RawMapViewDemoActivity.java` and `layout/raw_mapview_demo.xml`, that demonstrate how to directly use a `MapView`. The samples can be found in `\extras\google\google_play_services\samples`. – MH. Apr 05 '13 at 04:59
  • So I will need to override all the life cycle methods such as onCreate, onDestroy, onResume, onPause. but how do i override the oncreate method, I've update the question with my code. – Sam Apr 06 '13 at 07:09
  • is that ok for overide the oncreate method in fragment OncreateView. – Sam Apr 06 '13 at 07:15

0 Answers0