【发布时间】: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