本文介绍了在一个活动使用两个SupportMapFragment但无法从我第一SupportMapFragment添加二SupportMapFragment删除标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有奇怪的问题,而在一个活动使用两个SupportMapFragment在两个不同的片段。

I have strange issue while using Two SupportMapFragment in two different Fragment in one Activity.

其实我的问题是让说在OneFragment我有SupprtMapFragment加入,我在地图视图中添加10标记,现在我去详细页面中加入另一片段并且它也有SupprtMapFragment现在在这个片段,我想清楚并希望仅显示一个引脚,但它仍然显示11标记在地图上。

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 .

下面是我的源$ C ​​$ C:

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布局:

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>

这个片段有我的第一SupportMapFragment,我想表现出更多的则11标记超过地图,它工作得很好。

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屏幕截图:

OUTPUT Screen Shot:

现在,当您单击该按钮DetailMap将采取用户详细信息屏幕,在这里我想表明仅在SupportMapFragment一个标志,但我不知道为什么它仍然显示previous我在添加完所有标记我第一个片段。

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.

我的第二块碎片code:

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

            }

        }

    }

}

下面是home_map.xml我的XML code:

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>

另一个XML是在这里map_detail_screen.xml:

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 ?

任何一个可以给予妥善解决,所以当我在previous屏幕回去它都标记为保持相同,因为我已经添加他们。

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 .

推荐答案

替换

mSupportMapDetail=(SupportMapFragment) mFragMrg.findFragmentByTag("map_home_detail");

mSupportMapDetail=(SupportMapFragment) mFragMrg.findFragmentById("map_home_fragment_detail");

也可以尝试更换 findFragmentByTag findFragmentById 到处..

这篇关于在一个活动使用两个SupportMapFragment但无法从我第一SupportMapFragment添加二SupportMapFragment删除标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 09:05