Using Two SupportMapFragment in one Activity but u

2019-05-21 11:50发布

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: SupportMap One with 11 Marker

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:

SupportMap Second Screen

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 .

3条回答
Juvenile、少年°
2楼-- · 2019-05-21 11:58

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"/>
查看更多
家丑人穷心不美
3楼-- · 2019-05-21 12:10

Replace

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

with

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

Also try replacing findFragmentByTag to findFragmentById everywhere..

查看更多
一夜七次
4楼-- · 2019-05-21 12:12
// 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
查看更多
登录 后发表回答