Using Two SupportMapFragment in one Activity but unable remove Marker from Second SupportMapFragment that i have added in First SupportMapFragment

谁都会走 提交于 2019-12-06 12:28:23

Replace

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

with

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

Also try replacing findFragmentByTag to findFragmentById everywhere..

Replace this code to your second fragment:

private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();
        // Check if we were successful in obtaining the map.
        if (mMap != null) {
            setUpMap();
        }
    }
}

private void setUpMap() {
    // Hide the zoom controls as the button panel will cover it.
    mMap.getUiSettings().setZoomControlsEnabled(false);

// if there were any markers remove them just in case
mMap.clear();
    // Add your marker to the map
    addMarkersToMap();

    // Setting an info window adapter allows us to change the both the contents and look of the
    // info window.
    mMap.setInfoWindowAdapter(new CustomInfoWindowAdapter());

    // Set listeners for marker events.  See the bottom of this class for their behavior.
    mMap.setOnMarkerClickListener(this);
    mMap.setOnInfoWindowClickListener(this);
    mMap.setOnMarkerDragListener(this);

}

and in your map_detail_screen.xml add:

<fragment
  android:id="@+id/map_home_fragment_detail"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  class="com.google.android.gms.maps.SupportMapFragment"/>
// try this way,hope this will help you..
Note : you can not add one fragment inside another fragment.

**XML**
**activity_map**
<LinearLayout 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"
    tools:context=".MapActivity" >

    <FrameLayout
        android:id="@+id/frame_holder"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>

</LinearLayout>

**home_map**
<?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:gravity="center"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn_mapdetail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="MapDetails" />

    <ViewSwitcher
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:layout_marginTop="5dp"
        android:id="@+id/viewSwitcher">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical">
            <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"
                    android:text="textview"/>

            </LinearLayout>

            <FrameLayout
                android:id="@+id/fram_map_home_fragment"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_marginTop="5dp"
                android:layout_weight="1">
            </FrameLayout>
        </LinearLayout>
    </ViewSwitcher>
</LinearLayout>

**map_detail_screen**
<?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:background="@android:color/white"
    android:orientation="vertical">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical"
        android:gravity="center"
        android:layout_weight="1">
        <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/ic_launcher" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_launcher" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_launcher" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_launcher" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_launcher" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_launcher" />
        </LinearLayout>
    </ScrollView>
    <FrameLayout
        android:id="@+id/frame_detail"
        android:layout_width="match_parent"
        android:layout_height="350dp">
    </FrameLayout>
</LinearLayout>

**info_window_layout**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</LinearLayout>

**Activity**
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;


public class MyActivity 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();
    }
}

**MapHomeFragment**
package com.example.Demo;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ViewSwitcher;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import java.util.Timer;
import java.util.TimerTask;



public class MapHomeFragment extends Fragment implements GoogleMap.OnInfoWindowClickListener,View.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 Button mBtnMapDetail;
    private ViewSwitcher mViewSwitcher;
    private MapDetailFragment mMapDetailFragment;
    private FrameLayout fram_map_home_fragment;
    private Timer t;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        View mView=inflater.inflate(R.layout.home_map, container, false);
        fram_map_home_fragment = (FrameLayout) mView.findViewById(R.id.fram_map_home_fragment);
        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 = new SupportMapFragment();
        FragmentManager fm = getChildFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(fram_map_home_fragment.getId(), mSupportMapFragment);
        ft.commit();

        try{
            t = new Timer();
            t.schedule(new TimerTask() {

                @Override
                public void run() {
                    getActivity().runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                                if(mSupportMapFragment!=null){
                                    mMapView=mSupportMapFragment.getMap();
                                    if(mMapView!=null){
                                        mMapView.getUiSettings().setZoomControlsEnabled(true);
                                        mMapView.getUiSettings().setCompassEnabled(true);
                                        mMapView.getUiSettings().setZoomGesturesEnabled(true);
                                        mMapView.getUiSettings().setScrollGesturesEnabled(true);
                                        mMapView.getUiSettings().setRotateGesturesEnabled(true);
                                        mMapView.getUiSettings().setTiltGesturesEnabled(true);
                                        mMapView.setMyLocationEnabled(true);
                                        mMapView.setOnInfoWindowClickListener(MapHomeFragment.this);
                                        mMapView.setInfoWindowAdapter(new CustomInfoViewAdapter(getActivity()));

                                        MarkerOptions mMarkerOption=new MarkerOptions();
                                        mMarkerOption.title("Map One").position(mLatnogGuj).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)).snippet("My Name is");
                                        mMapView.addMarker(mMarkerOption);


                                        MarkerOptions mMarkerOption1=new MarkerOptions();
                                        mMarkerOption1.title("Map One").position(mLatnogGujOne).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)).snippet("My Name is");
                                        mMapView.addMarker(mMarkerOption1);

                                        MarkerOptions mMarkerOption2=new MarkerOptions();
                                        mMarkerOption2.title("Map One").position(mLatnogGujTwo).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)).snippet("My Name is");
                                        mMapView.addMarker(mMarkerOption2);

                                        MarkerOptions mMarkerOption3=new MarkerOptions();
                                        mMarkerOption3.title("Map One").position(mLatnogGuj3).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)).snippet("My Name is");
                                        mMapView.addMarker(mMarkerOption3);


                                        MarkerOptions mMarkerOption4=new MarkerOptions();
                                        mMarkerOption4.title("Map One").position(mLatnogGuj4).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)).snippet("My Name is");
                                        mMapView.addMarker(mMarkerOption4);

                                        MarkerOptions mMarkerOption5=new MarkerOptions();
                                        mMarkerOption5.title("Map One").position(mLatnogGuj5).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)).snippet("My Name is");
                                        mMapView.addMarker(mMarkerOption5);


                                        MarkerOptions mMarkerOption6=new MarkerOptions();
                                        mMarkerOption6.title("Map One").position(mLatnogGuj6).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)).snippet("My Name is");
                                        mMapView.addMarker(mMarkerOption6);


                                        MarkerOptions mMarkerOption7=new MarkerOptions();
                                        mMarkerOption7.title("Map One").position(mLatnogGuj7).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)).snippet("My Name is");
                                        mMapView.addMarker(mMarkerOption7);

                                        MarkerOptions mMarkerOption8=new MarkerOptions();
                                        mMarkerOption8.title("Map One").position(mLatnogGuj8).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)).snippet("My Name is");
                                        mMapView.addMarker(mMarkerOption8);

                                        MarkerOptions mMarkerOption9=new MarkerOptions();
                                        mMarkerOption9.title("Map One").position(mLatnogGuj9).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)).snippet("My Name is");
                                        mMapView.addMarker(mMarkerOption9);


                                        MarkerOptions mMarkerOption10=new MarkerOptions();
                                        mMarkerOption10.title("Map One").position(mLatnogGuj10).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)).snippet("My Name is");
                                        mMapView.addMarker(mMarkerOption10);

                                        mMapView.animateCamera(CameraUpdateFactory.newLatLngZoom(mLatnogGujOne,4));
                                        t.cancel();
                                    }
                                }
                        }
                    });

                }
            }, 0, 500);
        }catch (Exception e){
            e.printStackTrace();
        }

    }


    public class CustomInfoViewAdapter implements GoogleMap.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) {
    }


    @Override
    public void onClick(View v) {
        intiDetailFragment();
    }


    private void intiDetailFragment(){
        FragmentTransaction mFt=mFragMrg.beginTransaction();
        mMapDetailFragment=new MapDetailFragment();
        mFt.add(R.id.frame_holder, mMapDetailFragment, "DetailFragment").addToBackStack("DetailFragment").commit();
    }

}


**MapDetailFragment**
package com.example.Demo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import java.util.Timer;
import java.util.TimerTask;


public class MapDetailFragment extends Fragment {

    private FrameLayout frame_detail;
    private GoogleMap mGoogleMapView;
    private SupportMapFragment mSupportMapDetail;
    private LatLng mLatnogGujOne=new LatLng(18.9158, 72.8267);
    private Timer t;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        View mView = inflater.inflate(R.layout.map_detail_screen, container,false);
        frame_detail = (FrameLayout) mView.findViewById(R.id.frame_detail);
        intView();
        return mView;
    }

    private void intView() {
        mSupportMapDetail = new SupportMapFragment();
        FragmentManager fm = getChildFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(frame_detail.getId(), mSupportMapDetail);
        ft.commit();

        try{
            t = new Timer();
            t.schedule(new TimerTask() {

                @Override
                public void run() {
                    getActivity().runOnUiThread(new Runnable() {

                        @Override
                        public void run() {
                            if(mSupportMapDetail!=null){
                                mGoogleMapView=mSupportMapDetail.getMap();
                                if(mGoogleMapView!=null){
                                    mGoogleMapView.getUiSettings().setZoomControlsEnabled(true);
                                    mGoogleMapView.getUiSettings().setCompassEnabled(true);
                                    mGoogleMapView.getUiSettings().setZoomGesturesEnabled(true);
                                    mGoogleMapView.getUiSettings().setScrollGesturesEnabled(true);
                                    mGoogleMapView.getUiSettings().setRotateGesturesEnabled(true);
                                    mGoogleMapView.getUiSettings().setTiltGesturesEnabled(true);
                                    mGoogleMapView.setMyLocationEnabled(true);

                                    MarkerOptions mMarkerOption=new MarkerOptions();
                                    mMarkerOption.title("Map One").position(mLatnogGujOne).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE)).snippet("My Name is");
                                    mGoogleMapView.addMarker(mMarkerOption);
                                    mGoogleMapView.animateCamera(CameraUpdateFactory.newLatLngZoom(mLatnogGujOne,8));
                                    t.cancel();
                                }
                            }
                        }
                    });

                }
            }, 0, 500);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

**AndroidManifest.xml**

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.Demo"
  android:versionCode="1"
  android:versionName="1.0">

<uses-sdk android:minSdkVersion="8"
    android:targetSdkVersion="17"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>

<application
    android:label="@string/app_name"
    android:icon="@drawable/ic_launcher">
    <activity
        android:name="MyActivity"
        android:windowSoftInputMode="stateHidden|adjustResize"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>


    <meta-data
        android:name="com.google.android.gms.version"
        android:value="4030500" />
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="AIzaSyC2OdJ0WJtj5-iTAk_-MwjJNxgvFVdQoXg" />
  </application>
 </manifest>

![enter image description here][1]
![enter image description here][2]


  [1]: http://i.stack.imgur.com/6swQ2.png
  [2]: http://i.stack.imgur.com/wUWnA.png
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!