0

我正在尝试创建一个活动但有多个片段的应用程序。我正在使用 android.support.v4.* 来构建片段。到目前为止我通过扩展 Fragment 创建的每个项目,我想在我的应用程序中使用 google Map Api2,

到目前为止,我发现的所有教程都将“SupportMapFragment”作为元素添加到布局文件中。

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

但我不能使用这种方式,因为嵌套片段不支持,我如何在我的应用程序中使用谷歌地图 api2 作为片段

按照评论中的建议使用地图视图

 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);
        }
    }

布局

<?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>
4

0 回答 0