我正在尝试在我的地图上实现一个群集标记,它表现得有些奇怪,首先,它向我展示了群集标记,但也向我显示了点标记,这不应该发生,当我放大时群集标记仍然显示,我会添加一些图像来更好地解释它.
public class MapaViagem extends FragmentActivity { private GoogleMap GoogleMap; private String rm_IDViagem; private List<ClIEnteModel> mClIEnteModel = new ArrayList<ClIEnteModel>(); private List<EnderecoModel> mEnderecoModel = new ArrayList<EnderecoModel>(); private Viagemmodel mViagemmodel = new Viagemmodel(); private ClusterManager<MyItem> mClusterManager; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.maps); ArrayList<LatLng> coordList = new ArrayList<LatLng>(); try { Bundle parametros = getIntent().getExtras(); rm_IDViagem = parametros.getString("ID_viagem"); Repositorio ca = new Repositorio(this); mViagemmodel = ca.getViagemPorID(Integer.valueOf(rm_IDViagem)); Repositorio cl = new Repositorio(this); mClIEnteModel = cl.getClIEntesViagem(Integer.valueOf(rm_IDViagem)); System.out.println("TEM CLIENTE " + mClIEnteModel.size()); if(mClIEnteModel != null) { for (int i = 0; i < mClIEnteModel.size(); i++) { Repositorio mRepositorio = new Repositorio(this); mEnderecoModel = mRepositorio.getListaEnderecosDoClIEnte(Integer.valueOf(mClIEnteModel.get(i).getClIEntes_ID())); for (int j = 0; j < mEnderecoModel.size(); j++) { // Loading map initilizeMap(); // Changing map type GoogleMap.setMapType(GoogleMap.MAP_TYPE_norMAL); // Showing / hIDing your current location GoogleMap.setMyLocationEnabled(true); // Enable / disable zooming controls GoogleMap.getUiSettings().setZoomControlsEnabled(true); // Enable / disable my location button GoogleMap.getUiSettings().setMyLocationbuttonEnabled(true); // Enable / disable Compass icon GoogleMap.getUiSettings().setCompassEnabled(true); // Enable / disable Rotate gesture GoogleMap.getUiSettings().setRotateGesturesEnabled(true); // Enable / disable zooming functionality GoogleMap.getUiSettings().setZoomGesturesEnabled(true); final float latitude = float.parsefloat(mEnderecoModel.get(j).getLatitude()); final float longitude = float.parsefloat(mEnderecoModel.get(j).getLongitude()); coordList.add(new LatLng(latitude, longitude)); GoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 10)); mClusterManager = new ClusterManager<MyItem>(MapaViagem.this, GoogleMap); mClusterManager.setRenderer(new MyClusterRenderer(MapaViagem.this, GoogleMap, mClusterManager)); GoogleMap.setonCamerachangelistener(mClusterManager); GoogleMap.setonMarkerClickListener(mClusterManager); addItems(coordList); } } } } catch (Exception e) { e.printstacktrace(); } } public class MyClusterRenderer extends DefaultClusterRenderer<MyItem> { public MyClusterRenderer(Context context, GoogleMap map, ClusterManager<MyItem> clusterManager) { super(context, map, clusterManager); } @OverrIDe protected voID onBeforeClusterItemRendered(MyItem item, MarkerOptions markerOptions) { super.onBeforeClusterItemRendered(item, markerOptions);// item.setTitle(mClIEnteModel.get(i)); markerOptions.Title(item.getTitle()); } @OverrIDe protected voID onClusterItemRendered(MyItem clusterItem, Marker marker) { super.onClusterItemRendered(clusterItem, marker); //here you have access to the marker itself } @OverrIDe protected boolean shouldRenderAsCluster(Cluster<MyItem> cluster) { return cluster.getSize() > 3; // when count of markers is more than 3, render as cluster } } private voID addItems(List<LatLng> markers) { for (int i = 0; i < markers.size(); i++) { MyItem offsetItem = new MyItem(markers.get(i)); mClusterManager.addItem(offsetItem); } mClusterManager.cluster(); } private voID initilizeMap() { if (GoogleMap == null) { GoogleMap = ((MapFragment) getFragmentManager().findFragmentByID( R.ID.map)).getMap(); // check if map is created successfully or not if (GoogleMap == null) { Toast.makeText(getApplicationContext(), "Não foi possível carregar o mapa", Toast.LENGTH_SHORT) .show(); } } } @OverrIDe protected voID onResume() { super.onResume(); initilizeMap(); }}
模型:
public class MyItem implements ClusterItem { private LatLng mposition; private String Title; public MyItem(LatLng position){ mposition = position; } @OverrIDe public LatLng getposition() { return mposition; } public String getTitle() { return Title; } public voID setTitle(String Title) { this.Title = Title; }}
解决方法:
首先,你需要定义,当你的ClusterManager应该聚类时,你应该覆盖MyClusterRenderer中的方法:
@OverrIDeprotected boolean shouldRenderAsCluster(Cluster<MyItem> cluster) { return cluster.getSize() > 3; // when count of markers is more than 3, render as cluster}
其次,当您使用标记进行一些更改(例如添加,删除,更改标题,图标,位置)时,应调用cluster()方法.所以你的addItems方法应如下所示:
private voID addItems(List<LatLng> markers) { for (int i = 0; i < markers.size(); i++) { MyItem offsetItem = new MyItem(markers.get(i)); mClusterManager.addItem(offsetItem); } mClusterManager.cluster();}
编辑:
我再一次看你的代码,发现你忘了设置聚类算法,它应该是:
... mClusterManager = new ClusterManager<MyItem>(MapaViagem.this, GoogleMap); mClusterManager.setAlgorithm(new GrIDBasedAlgorithm<MyItem>()); mClusterManager.setRenderer(new MyClusterRenderer(MapaViagem.this, GoogleMap, mClusterManager)); ...
总结 以上是内存溢出为你收集整理的android – 放大时不会隐藏群集标记全部内容,希望文章能够帮你解决android – 放大时不会隐藏群集标记所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)