出于不同目的复制谷歌地图视图片段



我有一个列表视图,在一个片段上有三个项目,在item上单击第一个项目,它用包含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


}

}
});

假设您有一个片段或活动同时包含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
}

最新更新