自定义标记不聚类
Custom markers not clustering
我的自定义标记有问题,我想把它做成一个集群标记(一组制造商),但它不起作用,我在 Google 地图中找到了所有标记,所以即使我使用 clusterManager 来处理这种情况,我也无法获得想要的结果:
public MyClusterManagerRenderer(Context context, GoogleMap googleMap,
ClusterManager<ClusterMarker> clusterManager) {//initialize
}
@Override
protected void onBeforeClusterItemRendered(ClusterMarker item, MarkerOptions markerOptions) {
imageView.setImageResource(item.getIconPicture());
Bitmap icon = iconGenerator.makeIcon();
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(icon)).title(item.getTitle());
}
@Override
protected void onBeforeClusterRendered(Cluster<ClusterMarker> cluster,
MarkerOptions markerOptions) {
iconGenerator.setBackground(null);
Bitmap icon = iconGenerator.makeIcon(String.valueOf(cluster
.getSize()));
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(icon));
}
@Override
protected boolean shouldRenderAsCluster(Cluster cluster) {
return false;
}
在我的 GoogleMapsFragment 中,我用这种方法来绘制标记:
private void addMapMarkers(List<Place> places){
if(places != null && !places.isEmpty() && googleMap != null)
{
mClusterManager = new ClusterManager<ClusterMarker>(getActivity(), googleMap);
mClusterManagerRenderer = new MyClusterManagerRenderer(getActivity(),googleMap,mClusterManager);
mClusterManager.setRenderer(mClusterManagerRenderer);
for(Place p: places){
try{
ClusterMarker newClusterMarker = null;
if(p != null)
{
newClusterMarker = new ClusterMarker(new LatLng(Double.parseDouble(p.getLocation().getLat()),
Double.parseDouble(p.getLocation().getLng())), p.getName(), snippet,
avatar, p );
}
if (newClusterMarker != null)
{
mClusterManager.addItem(newClusterMarker);
mClusterMarkers.add(newClusterMarker);
}
}catch (NullPointerException e){
Log.e(TAG, "7 : "+"addMapMarkers: NullPointerException: " + e.getMessage() );
}
}
mClusterManager.cluster();
googleMap.moveCamera(prepareCameraForMap(places));
}
else
Log.e(TAG, "9 : Markers not drawed googleMap is null"); }
我删除了这个方法:
shouldRenderAsCluster(Cluster cluster)
从 MyClusterManagerRenderer
然后,我的代码工作正常。
我的自定义标记有问题,我想把它做成一个集群标记(一组制造商),但它不起作用,我在 Google 地图中找到了所有标记,所以即使我使用 clusterManager 来处理这种情况,我也无法获得想要的结果:
public MyClusterManagerRenderer(Context context, GoogleMap googleMap,
ClusterManager<ClusterMarker> clusterManager) {//initialize
}
@Override
protected void onBeforeClusterItemRendered(ClusterMarker item, MarkerOptions markerOptions) {
imageView.setImageResource(item.getIconPicture());
Bitmap icon = iconGenerator.makeIcon();
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(icon)).title(item.getTitle());
}
@Override
protected void onBeforeClusterRendered(Cluster<ClusterMarker> cluster,
MarkerOptions markerOptions) {
iconGenerator.setBackground(null);
Bitmap icon = iconGenerator.makeIcon(String.valueOf(cluster
.getSize()));
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(icon));
}
@Override
protected boolean shouldRenderAsCluster(Cluster cluster) {
return false;
}
在我的 GoogleMapsFragment 中,我用这种方法来绘制标记:
private void addMapMarkers(List<Place> places){ if(places != null && !places.isEmpty() && googleMap != null) { mClusterManager = new ClusterManager<ClusterMarker>(getActivity(), googleMap); mClusterManagerRenderer = new MyClusterManagerRenderer(getActivity(),googleMap,mClusterManager); mClusterManager.setRenderer(mClusterManagerRenderer); for(Place p: places){ try{ ClusterMarker newClusterMarker = null; if(p != null) { newClusterMarker = new ClusterMarker(new LatLng(Double.parseDouble(p.getLocation().getLat()),
Double.parseDouble(p.getLocation().getLng())), p.getName(), snippet, avatar, p ); }
if (newClusterMarker != null) { mClusterManager.addItem(newClusterMarker); mClusterMarkers.add(newClusterMarker); } }catch (NullPointerException e){ Log.e(TAG, "7 : "+"addMapMarkers: NullPointerException: " + e.getMessage() ); } } mClusterManager.cluster(); googleMap.moveCamera(prepareCameraForMap(places)); } else Log.e(TAG, "9 : Markers not drawed googleMap is null"); }
我删除了这个方法:
shouldRenderAsCluster(Cluster cluster)
从 MyClusterManagerRenderer
然后,我的代码工作正常。