I have strange issue while using Two SupportMapFragment in two different Fragment in one Activity.
Actually my problem is let say In OneFragment i have SupprtMapFragment added and i add 10 Marker in that map view and now i go to detail page by adding another Fragment and in that it have also SupprtMapFragment now in this Fragment i want to clear it and want to show Only one pin but it Still Showing 11 Marker over map .
Here is my Source code:
public class MapActivity extends FragmentActivity {
private FragmentManager mFragmrg;
public static String TAG_FRAGMENT_HOME="HomeFragment";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
initView();
}
private void initView(){
mFragmrg=getSupportFragmentManager();
MapHomeFragment mMapHomeFragment=new MapHomeFragment();
FragmentTransaction mFt=mFragmrg.beginTransaction();
mFt.replace(R.id.frame_holder, mMapHomeFragment, TAG_FRAGMENT_HOME).addToBackStack(TAG_FRAGMENT_HOME).commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.map, menu);
return true;
}
}
XMl Layout of Activity:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MapActivity" >
<FrameLayout
android:id="@+id/frame_holder"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</RelativeLayout>
THis Fragment have my First SupportMapFragment where i want to show more then 11 Marker over map and it work well.
public class MapHomeFragment extends Fragment implements OnInfoWindowClickListener,OnClickListener{
private GoogleMap mMapView;
private FragmentManager mFragMrg;
private SupportMapFragment mSupportMapFragment;
private LatLng mLatnogGuj=new LatLng(23.0626, 72.5809);
private LatLng mLatnogGujOne=new LatLng(18.9158, 72.8267);
private LatLng mLatnogGujTwo=new LatLng(24.5925, 72.7156);
private LatLng mLatnogGuj3=new LatLng(34.0837,74.7974);
private LatLng mLatnogGuj4=new LatLng(23.2245, 72.6379);
private LatLng mLatnogGuj5=new LatLng(12.9716, 77.5946);
private LatLng mLatnogGuj6=new LatLng(23.0396, 72.5660);
private LatLng mLatnogGuj7=new LatLng(22.3140, 73.1748);
private LatLng mLatnogGuj8=new LatLng(20.5732, 73.7442);
private LatLng mLatnogGuj9=new LatLng(21.1785, 72.8191);
private LatLng mLatnogGuj10=new LatLng(28.6644, 77.2349);
private LatLngBounds.Builder mLatlongBuilder;
private Button mBtnMapDetail;
private ViewSwitcher mViewSwitcher;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View mView=inflater.inflate(R.layout.home_map, container, false);
initView(mView);
return mView;
}
private void initView(View mView){
mViewSwitcher=(ViewSwitcher)mView.findViewById(R.id.viewSwitcher);
mViewSwitcher.showNext();
try{
MapsInitializer.initialize(getActivity());
}catch(Exception Exc){
Exc.printStackTrace();
}
mBtnMapDetail=(Button)mView.findViewById(R.id.btn_mapdetail);
mBtnMapDetail.setOnClickListener(this);
mFragMrg=getFragmentManager();
mSupportMapFragment=(SupportMapFragment) mFragMrg.findFragmentByTag("map_home_fragment");
if(mSupportMapFragment!=null){
mMapView=mSupportMapFragment.getMap();
if(mMapView!=null){
//mMapView.getUiSettings().setAllGesturesEnabled(true);
mMapView.getUiSettings().setMyLocationButtonEnabled(true);
mMapView.getUiSettings().setZoomGesturesEnabled(true);
mMapView.getUiSettings().setZoomControlsEnabled(true);
mMapView.setOnInfoWindowClickListener(this);
mMapView.setInfoWindowAdapter(new CustomInfoViewAdapter(getActivity()));
mLatlongBuilder=new LatLngBounds.Builder();
MarkerOptions mMarkerOption=new MarkerOptions();
mMarkerOption.title("Map One").position(mLatnogGuj).icon(BitmapDescriptorFactory.fromResource(R.drawable.map)).snippet("My Name is");
mLatlongBuilder.include(mLatnogGuj);
mMapView.addMarker(mMarkerOption);
MarkerOptions mMarkerOption1=new MarkerOptions();
mMarkerOption1.title("Map One").position(mLatnogGujOne).icon(BitmapDescriptorFactory.fromResource(R.drawable.map_pub)).snippet("My Name is");
mLatlongBuilder.include(mLatnogGujOne);
mMapView.addMarker(mMarkerOption1);
MarkerOptions mMarkerOption2=new MarkerOptions();
mMarkerOption2.title("Map One").position(mLatnogGujTwo).icon(BitmapDescriptorFactory.fromResource(R.drawable.map_rest)).snippet("My Name is");
mLatlongBuilder.include(mLatnogGujTwo);
mMapView.addMarker(mMarkerOption2);
MarkerOptions mMarkerOption3=new MarkerOptions();
mMarkerOption3.title("Map One").position(mLatnogGuj3).icon(BitmapDescriptorFactory.fromResource(R.drawable.map_small)).snippet("My Name is");
mLatlongBuilder.include(mLatnogGuj3);
mMapView.addMarker(mMarkerOption3);
MarkerOptions mMarkerOption4=new MarkerOptions();
mMarkerOption4.title("Map One").position(mLatnogGuj4).icon(BitmapDescriptorFactory.fromResource(R.drawable.map_take)).snippet("My Name is");
mLatlongBuilder.include(mLatnogGuj4);
mMapView.addMarker(mMarkerOption4);
MarkerOptions mMarkerOption5=new MarkerOptions();
mMarkerOption5.title("Map One").position(mLatnogGuj5).icon(BitmapDescriptorFactory.fromResource(R.drawable.map_top)).snippet("My Name is");
mLatlongBuilder.include(mLatnogGuj5);
mMapView.addMarker(mMarkerOption5);
MarkerOptions mMarkerOption6=new MarkerOptions();
mMarkerOption6.title("Map One").position(mLatnogGuj6).icon(BitmapDescriptorFactory.fromResource(R.drawable.map_top_hover)).snippet("My Name is");
mLatlongBuilder.include(mLatnogGuj6);
mMapView.addMarker(mMarkerOption6);
MarkerOptions mMarkerOption7=new MarkerOptions();
mMarkerOption7.title("Map One").position(mLatnogGuj7).icon(BitmapDescriptorFactory.fromResource(R.drawable.map_pub)).snippet("My Name is");
mLatlongBuilder.include(mLatnogGuj7);
mMapView.addMarker(mMarkerOption7);
MarkerOptions mMarkerOption8=new MarkerOptions();
mMarkerOption8.title("Map One").position(mLatnogGuj8).icon(BitmapDescriptorFactory.fromResource(R.drawable.map_rest)).snippet("My Name is");
mLatlongBuilder.include(mLatnogGuj8);
mMapView.addMarker(mMarkerOption8);
MarkerOptions mMarkerOption9=new MarkerOptions();
mMarkerOption9.title("Map One").position(mLatnogGuj9).icon(BitmapDescriptorFactory.fromResource(R.drawable.map_small)).snippet("My Name is");
mMapView.addMarker(mMarkerOption9);
mLatlongBuilder.include(mLatnogGuj9);
MarkerOptions mMarkerOption10=new MarkerOptions();
mMarkerOption10.title("Map One").position(mLatnogGuj10).icon(BitmapDescriptorFactory.fromResource(R.drawable.map_take)).snippet("My Name is");
mLatlongBuilder.include(mLatnogGuj10);
mMapView.addMarker(mMarkerOption10);
//mMapView.animateCamera(CameraUpdateFactory.newLatLngBounds(mLatlongBuilder.build(), 120));
mBtnMapDetail.postDelayed(new Runnable() {
@Override
public void run() {
mMapView.animateCamera(CameraUpdateFactory.newLatLngBounds(mLatlongBuilder.build(), 120));
}
}, 250);
}
}
}
public class CustomInfoViewAdapter implements InfoWindowAdapter{
View mLayoutInfo;
public CustomInfoViewAdapter(Context _Context){
LayoutInflater mLayoutInflater=(LayoutInflater) _Context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mLayoutInfo=mLayoutInflater.inflate(R.layout.info_window_layout, null);
}
@Override
public View getInfoContents(Marker marker) {
return mLayoutInfo;
}
@Override
public View getInfoWindow(Marker marker) {
return null;
}
}
@Override
public void onInfoWindowClick(Marker arg0) {
// TODO Auto-generated method stub
}
@Override
public void onClick(View v) {
intiDetailFragment();
}
private void intiDetailFragment(){
MapDetailFragment mMapDetailFragment=new MapDetailFragment();
FragmentTransaction mFt=mFragMrg.beginTransaction();
mFt.add(R.id.frame_holder, mMapDetailFragment, "DetailFragment").addToBackStack("DetailFragment").commit();
}
}
OUTPUT Screen Shot:
Now when you click that DetailMap button it will take user to Detail Screen where i want to show only One Marker in that SupportMapFragment but i don't known why it still Showing previous all Marker which i have added in my First Fragment.
My Second Fragment Code:
public class MapDetailFragment extends Fragment {
private GoogleMap mGoogleMapView;
private SupportMapFragment mSupportMapDetail;
private FragmentManager mFragMrg;
private LatLng mLatnogGujOne=new LatLng(18.9158, 72.8267);
private LatLngBounds.Builder mLatlongBuilder;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View mView = inflater.inflate(R.layout.map_detail_screen, container,
false);
intView(mView);
return mView;
}
private void intView(View mView) {
mFragMrg=getChildFragmentManager();
mSupportMapDetail=(SupportMapFragment) mFragMrg.findFragmentByTag("map_home_detail");
if(mSupportMapDetail!=null){
mGoogleMapView=mSupportMapDetail.getMap();
if(mGoogleMapView!=null){
mGoogleMapView.clear();
mGoogleMapView.getUiSettings().setAllGesturesEnabled(true);
mGoogleMapView.getUiSettings().setMyLocationButtonEnabled(true);
mGoogleMapView.getUiSettings().setZoomGesturesEnabled(true);
mGoogleMapView.getUiSettings().setZoomControlsEnabled(true);
mGoogleMapView.setMyLocationEnabled(true);
mLatlongBuilder=new LatLngBounds.Builder();
MarkerOptions mMarkerOption=new MarkerOptions();
mMarkerOption.title("Map One").position(mLatnogGujOne).icon(BitmapDescriptorFactory.fromResource(R.drawable.map_pub)).snippet("My Name is");
mLatlongBuilder.include(mLatnogGujOne);
mGoogleMapView.addMarker(mMarkerOption);
mGoogleMapView.animateCamera(CameraUpdateFactory.newLatLngBounds(mLatlongBuilder.build(), 120));
}
}
}
}
Here is my xml code of home_map.xml :
<?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" >
<Button
android:id="@+id/btn_mapdetail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/mapdetail" />
<ViewSwitcher
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/viewSwitcher"
>
<LinearLayout
android:id="@+id/llMapDemo"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<fragment
android:id="@+id/map_home_fragment"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:tag="map_home_fragment" />
</ViewSwitcher>
</LinearLayout>
another xml is here map_detail_screen.xml :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:clickable="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/empty_photo" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/empty_photo" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/empty_photo" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/empty_photo" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/empty_photo" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/empty_photo" />
<FrameLayout
android:id="@+id/frame_detail"
android:layout_width="match_parent"
android:layout_height="350dp" >
<fragment
android:id="@+id/map_home_fragment_detail"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:tag="map_home_detail" />
</FrameLayout>
</LinearLayout>
</ScrollView>
Here is my Second screen output:
As you can see in my second screen show it still showing Marker on map Why this happen ?
Can any one give proper solution so when i go back in previous screen it have all Marker remain as same as i have added them .
Replace this code to your second fragment:
}
and in your map_detail_screen.xml add:
Replace
with
Also try replacing
findFragmentByTag
tofindFragmentById
everywhere..