【问题标题】:Duplicating Google MapView fragment for different purpose为不同目的复制 Google MapView 片段
【发布时间】:2018-01-22 09:02:28
【问题描述】:

我有一个列表视图,其中一个片段上有三个项目,并且在第一个项目的 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);



    }
} 

Listviews 的 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




                }



            }
        });

【问题讨论】:

  • 你的意思是每个项目都需要在地图上显示不同的标记吗?在这种情况下,您根本不需要替换片段。只需删除并放置一个新标记
  • @vlatkozelka 这是否意味着在不同位置创建另一个带有标记的片段?
  • 不,恰恰相反。在点击侦听器本身之外的开头创建地图片段。当您单击列表中的一个项目时,创建一个标记并将其放在地图上,当您单击另一个时,删除第一个标记并放置另一个。
  • @vlatkozelka 你能举个例子吗?

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


【解决方案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

  }

【讨论】:

    猜你喜欢
    • 2015-07-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-28
    • 1970-01-01
    • 2018-05-06
    • 2011-07-03
    • 2016-04-18
    • 2022-10-17
    相关资源
    最近更新 更多