I ran into the same problem, but finally I dealt with it that way.
First of all, I create a separate class MyMapFragment, which extends android.support.v4.app.Fragment
here the onCreateView method is used
if (view != null) { ViewGroup parent = (ViewGroup) view.getParent(); if (parent != null) parent.removeView(view); } try { view = (ViewGroup) inflater.inflate(R.layout.map_frag, container, false); setUpMapIfNeeded(); showMap(maptype); } catch (Exception e) { } return view;
after that i create one xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/my_fragmentView" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > </LinearLayout>
You can add this layout file to the map location.
and finally you need to add a map view using this one in its activity class
MyMapFragment newFragment = new MyMapFragment (1); FragmentTransaction transaction = getActivity() .getSupportFragmentManager().beginTransaction(); transaction.add(R.id.my_fragmentView, newFragment); transaction.commit();
It works great on my side, hope this helps you.
MapFragment Code here
public class MapFragment extends Fragment { private View view; private GoogleMap mMapView; public TouchableWrapper mTouchView; LinearLayout map_fragg; String userLat, userLon; int maptype = 1; Marker lastMarker = null; public MapFragment() { } public MapFragment(int maptype) { this.maptype = maptype; } @Override public void onAttach(Activity activity) { super.onAttach(activity); getView(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (container == null) { return null; } if (view != null) { ViewGroup parent = (ViewGroup) view.getParent(); if (parent != null) parent.removeView(view); if (mTouchView != null) { mTouchView.removeAllViews(); } } try { mTouchView = new TouchableWrapper(getActivity()); view = (ViewGroup) inflater.inflate(R.layout.map_frag, null, false); view.setLayoutParams(new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); map_fragg = (LinearLayout) view.findViewById(R.id.map_fragg); mTouchView.addView(view); setUpMapIfNeeded(); } catch (Exception e) { e.printStackTrace(); } return mTouchView; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } private void showMap(int maptype) { switch (maptype) { case 1: setMapX(); break; case 2: setMapXX(); break; case 3: setUpMapXXX(); break; default: break; } } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); } private void setUpMapIfNeeded() {
source share