为不同目的复制Google MapView片段

时间:2017-08-14 13:43:06

标签: android google-maps android-fragments google-maps-markers

我有一个列表视图,其中包含一个片段上的三个项目,以及第一个项目的itemClick,它将替换为包含mapView的下一个片段。(mapView将标记放置在一个位置)。现在我的问题是:在单击列表视图的第二项或第三项时,如何使用相同的MapView片段在不同位置显示带有标记的地图?这是我的MapViewFragment:

public class MapViewFragment extends Fragment implements OnMapReadyCallback {
    GoogleMap mGoogleMap;
    MapView mMapView;
    View mView;




    public MapViewFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        mView = inflater.inflate(R.layout.fragment_map_view, container, false);
        return mView;
    }



    @Override
    public void onViewCreated(View view,  Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mMapView = (MapView) mView.findViewById(R.id.map);

        if (mMapView != null){
            mMapView.onCreate(null);
            mMapView.onResume();
            mMapView.getMapAsync(this);

        }
    }

    //method of OnMapReadyCallBack Interface


    public void onMapReady(GoogleMap googleMap) {
        MapsInitializer.initialize(this.getActivity());
        mGoogleMap = googleMap;
        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        googleMap.addMarker(new MarkerOptions()
                .position(new LatLng(27.619619, 85.538637))
                .title("Kathmandu University Dhulikhel Kavre")
                .snippet("Detalis here")
        );

        LatLng latLng = new LatLng(27.619619, 85.538637);

        googleMap.setInfoWindowAdapter(new MapInfoItem(getActivity().getLayoutInflater()));

        googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 16));

        googleMap.setMyLocationEnabled(true);



    }
} 

用于列表视图的ItemClickListener:

 list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                switch (position){
                    case 0:
                        FragmentManager manager = getFragmentManager();
                        manager.beginTransaction().replace(R.id.content_main, new MapViewFragment()).addToBackStack(null).commit();


                        break;
                    case 1:
//what do I do here
                    case 2:
//what do I do here




                }



            }
        });

1 个答案:

答案 0 :(得分:1)

假设您有一个片段或活动同时持有MapFragment和listView。您需要做的就是访问地图对象,并在点击时添加/删除标记。你已经完成了大部分工作。

public class MapViewFragment extends Fragment implements OnMapReadyCallback {
    GoogleMap mGoogleMap;
    MapView mMapView;
    View mView;




    public MapViewFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        mView = inflater.inflate(R.layout.fragment_map_view, container, false);
        return mView;
    }



    @Override
    public void onViewCreated(View view,  Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mMapView = (MapView) mView.findViewById(R.id.map);

        if (mMapView != null){
            mMapView.onCreate(null);
            mMapView.onResume();
            mMapView.getMapAsync(this);

        }
    }

    //method of OnMapReadyCallBack Interface


    public void onMapReady(GoogleMap googleMap) {
        MapsInitializer.initialize(this.getActivity());
        mGoogleMap = googleMap;
        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        googleMap.addMarker(new MarkerOptions()
                .position(new LatLng(27.619619, 85.538637))
                .title("Kathmandu University Dhulikhel Kavre")
                .snippet("Detalis here")
        );

        LatLng latLng = new LatLng(27.619619, 85.538637);

        googleMap.setInfoWindowAdapter(new MapInfoItem(getActivity().getLayoutInflater()));

        googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 16));

        googleMap.setMyLocationEnabled(true);



    }

 //Add this method to get access to the map from outside the fragment
 public GoogleMap getGoogleMap(){
    return mGoogleMap;
 }
} 

现在在您点击监听器的父活动/片段内,首先通过调用:

放置片段
FragmentManager manager = getFragmentManager();
manager.beginTransaction().replace(R.id.content_main, new MapViewFragment()).addToBackStack(null).commit();

在Activity onCreate()或framgent onCreateView

然后在你编写的点击监听器中,保留相同的地图,只需替换标记:

 Marker marker;// keep a reference to your marker

 list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {

            switch (position){
                case 0:
                gotoLocation1();
                    break;
                case 1:
                 gotoLocation2();
                    break;
                case 2:
                 gotoLocation3();
                    break;
          }
    });

  private void gotoLocation1(){
   GoogleMap googleMap = youMapFragment.getGoogleMap();
            if(googleMap == null){
             return; // your map might not have loaded yet
            }

            if(marker != null)
               marker.remove();

   googleMap.addMarker(new MarkerOptions()
            .position(new LatLng(27.619619, 85.538637))
            .title("Kathmandu University Dhulikhel Kavre")
            .snippet("Detalis here")
    );

    LatLng latLng = new LatLng(27.619619, 85.538637);

    googleMap.setInfoWindowAdapter(new MapInfoItem(getActivity().getLayoutInflater()));

    googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 16));
}

// now do the same for gotoLocation2() and gotoLlocation3() just chaning the lat lng

  }