Android 地图 ClusteredMarkers - 不显示非聚类?
Android Maps ClusteredMarkers - don't show unclustered?
我正在使用来自 here 的 Google 地图 Android 标记聚类实用程序,并且我正在从我从服务器收到的列表中添加一堆项目(大约 700 个) .我已经解决了我最初的错误,我可以看到集群,然后放大它们并看到它们分解成单独的标记。然而,有几个项目与其他项目相距很远,因此即使在最大程度缩小时,它们也永远不会聚集在一起。出于某种原因,这些项目没有显示在我的地图上 - 放大时不显示,缩小时不显示。
我已经检查了坐标,它们是真实的,在我开始使用聚类之前我可以看到这些项目没有问题,所以我假设我在我的聚类代码中做错了。
这是一些代码:
private void setUpClusterer() {
// Initialize the manager with the context and the map
mClusterManager = new ClusterManager<>(getActivity(), map);
vendorRenderer = new VendorRenderer();
mClusterManager.setRenderer(vendorRenderer);
mClusterManager.setOnClusterClickListener(this);
mClusterManager.setOnClusterItemClickListener(this);
//point the maps listeners at the listeners implemented by the cluster manager
map.setOnCameraChangeListener(mClusterManager);
map.setOnMarkerClickListener(mClusterManager);
//add items to the cluster manager
addClusterItems(-1);
mClusterManager.cluster();
}
private void addClusterItems(int positionToMark) {
if (null == list) {
return;
}
LatLng position;
int maxMarkers = Math.min(list.size(), getResources().getInteger(R.integer.number_of_results_on_map));
mClusterManager.clearItems();
for (int i = 0; i < maxMarkers; i++) {
vendorItem = list.get(i);
if (vendorItem.getAddress().contains("Remote 1")) {
Log.e("Kibi", "Adding Remote 1, pos = " + i);
Log.e("Kibi", "Coordinates =" + vendorItem.getPointCoordinates().toString());
}
if (vendorItem.getAddress().contains("Clustered 1")) {
Log.e("Kibi", "Adding Clustered 1, pos = " + i);
Log.e("Kibi", "Coordinates =" + vendorItem.getPointCoordinates().toString());
}
if (vendorItem.getAddress().contains("Remote 2")) {
Log.e("Kibi", "Adding Remote 2, pos = " + i);
Log.e("Kibi", "Coordinates =" + vendorItem.getPointCoordinates().toString());
}
VendorMapItem item = new VendorMapItem(vendorItem.getPointCoordinates(),
"Some other text");
if (i == positionToMark) {
selectedItem = item;
}
mClusterManager.addItem(item);
}
if (-1 == positionToMark) {
selectedItem = null;
}
}
这显示了添加的项目 - 我添加的日志帮助我看到我添加的 2 个远程项目具有良好的坐标,并且看起来与我选择的集群项目(可见)相似(虽然距离很远)
渲染代码如下:
/* This draws the markers for us */
private class VendorRenderer extends DefaultClusterRenderer<VendorMapItem> {
Context context = getActivity().getApplicationContext();
public final IconGenerator mIconGenerator = new IconGenerator(context);
public final IconGenerator mSelectedIconGenerator = new IconGenerator(context);
private final View mItemView;
private final View mSelectedItemView;
public VendorRenderer() {
super(getActivity().getApplicationContext(), map, mClusterManager);
// Create selected custom Marker
RelativeLayout selectedContainer = (RelativeLayout) view.findViewById(R.id.marker_map_selected_container);
mSelectedItemView = ((LayoutInflater) getActivity().getSystemService
(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.map_marker_selected_layout, selectedContainer, false);
mSelectedIconGenerator.setContentView(mSelectedItemView);
mSelectedIconGenerator.setBackground(null);
// Create custom Marker
LinearLayout container = (LinearLayout) view.findViewById(R.id.text_marker_map_container);
mItemView = ((LayoutInflater) getActivity().getSystemService
(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.map_marker_layout, container, true);
mIconGenerator.setContentView(mItemView);
mIconGenerator.setBackground(null);
}
@Override
protected void onBeforeClusterItemRendered(VendorMapItem vendor, MarkerOptions markerOptions) {
// Draw a single vendor.
Bitmap icon;
if (null == selectedItem || !vendor.getPosition().equals(selectedItem.getPosition()))
{
icon = mIconGenerator.makeIcon();
} else {
icon = mSelectedIconGenerator.makeIcon();
}
markerOptions.title(vendor.getTitle());
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(icon));
}
@Override
protected void onBeforeClusterRendered(Cluster<VendorMapItem> cluster, MarkerOptions markerOptions) {
// Draw multiple vendors clustered...
Bitmap icon = mIconGenerator.makeIcon();
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(icon));
}
@Override
protected boolean shouldRenderAsCluster(Cluster cluster) {
// Always render clusters.
return cluster.getSize() > 1;
}
}
通常会显示聚类项目,也会显示非聚类项目,无论是否选中。我有另一个视图显示列表中的所有位置数据,而我的远程位置只显示在那里。
知道我做错了什么吗?
public class ClusterIconProvider 实现 IconDataProvider {
private static final int[] res = {R.drawable.m1,R.drawable.m2,R.drawable.m3,R.drawable.m4,R.drawable.m5};
private static final int[] forCounts = { 10, 100, 1000, 10000, Integer.MAX_VALUE };
private Bitmap[] baseBitmaps;
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private Rect bounds = new Rect();
private MarkerOptions markerOptions = new MarkerOptions().anchor(0.5f, 0.5f);
public ClusterIconProvider(Resources resources){
baseBitmaps = new Bitmap[res.length];
for (int i = 0; i < res.length; i++) {
baseBitmaps[i] = BitmapFactory.decodeResource(resources, res[i]);
}
paint.setColor(Color.WHITE);
paint.setTextAlign(Align.CENTER);
paint.setTextSize(resources.getDimension(R.dimen.Cluster));
}
@Override
public MarkerOptions getIconData(int markersCount) {
// TODO Auto-generated method stub
Bitmap base;
int i = 0;
do {
base = baseBitmaps[i];
} while (markersCount >= forCounts[i++]);
Bitmap bitmap = base.copy(Config.ARGB_8888, true);
String text = String.valueOf(markersCount);
paint.getTextBounds(text, 0, text.length(), bounds);
float x = bitmap.getWidth() / 2.0f;
float y = (bitmap.getHeight() - bounds.height()) / 2.0f - bounds.top;
Canvas canvas = new Canvas(bitmap);
canvas.drawText(text, x, y, paint);
BitmapDescriptor icon = BitmapDescriptorFactory.fromBitmap(bitmap);
return markerOptions.icon(icon);
}
}
愚蠢。
基本上我调用 map.clear() 的次数太多了。我想如果地图上绘制的所有内容都是由聚类器控制的引脚,则无需调用 clear。一旦删除了 clear() 调用,我的图钉就会再次出现。
我正在使用来自 here 的 Google 地图 Android 标记聚类实用程序,并且我正在从我从服务器收到的列表中添加一堆项目(大约 700 个) .我已经解决了我最初的错误,我可以看到集群,然后放大它们并看到它们分解成单独的标记。然而,有几个项目与其他项目相距很远,因此即使在最大程度缩小时,它们也永远不会聚集在一起。出于某种原因,这些项目没有显示在我的地图上 - 放大时不显示,缩小时不显示。
我已经检查了坐标,它们是真实的,在我开始使用聚类之前我可以看到这些项目没有问题,所以我假设我在我的聚类代码中做错了。
这是一些代码:
private void setUpClusterer() {
// Initialize the manager with the context and the map
mClusterManager = new ClusterManager<>(getActivity(), map);
vendorRenderer = new VendorRenderer();
mClusterManager.setRenderer(vendorRenderer);
mClusterManager.setOnClusterClickListener(this);
mClusterManager.setOnClusterItemClickListener(this);
//point the maps listeners at the listeners implemented by the cluster manager
map.setOnCameraChangeListener(mClusterManager);
map.setOnMarkerClickListener(mClusterManager);
//add items to the cluster manager
addClusterItems(-1);
mClusterManager.cluster();
}
private void addClusterItems(int positionToMark) {
if (null == list) {
return;
}
LatLng position;
int maxMarkers = Math.min(list.size(), getResources().getInteger(R.integer.number_of_results_on_map));
mClusterManager.clearItems();
for (int i = 0; i < maxMarkers; i++) {
vendorItem = list.get(i);
if (vendorItem.getAddress().contains("Remote 1")) {
Log.e("Kibi", "Adding Remote 1, pos = " + i);
Log.e("Kibi", "Coordinates =" + vendorItem.getPointCoordinates().toString());
}
if (vendorItem.getAddress().contains("Clustered 1")) {
Log.e("Kibi", "Adding Clustered 1, pos = " + i);
Log.e("Kibi", "Coordinates =" + vendorItem.getPointCoordinates().toString());
}
if (vendorItem.getAddress().contains("Remote 2")) {
Log.e("Kibi", "Adding Remote 2, pos = " + i);
Log.e("Kibi", "Coordinates =" + vendorItem.getPointCoordinates().toString());
}
VendorMapItem item = new VendorMapItem(vendorItem.getPointCoordinates(),
"Some other text");
if (i == positionToMark) {
selectedItem = item;
}
mClusterManager.addItem(item);
}
if (-1 == positionToMark) {
selectedItem = null;
}
}
这显示了添加的项目 - 我添加的日志帮助我看到我添加的 2 个远程项目具有良好的坐标,并且看起来与我选择的集群项目(可见)相似(虽然距离很远)
渲染代码如下:
/* This draws the markers for us */
private class VendorRenderer extends DefaultClusterRenderer<VendorMapItem> {
Context context = getActivity().getApplicationContext();
public final IconGenerator mIconGenerator = new IconGenerator(context);
public final IconGenerator mSelectedIconGenerator = new IconGenerator(context);
private final View mItemView;
private final View mSelectedItemView;
public VendorRenderer() {
super(getActivity().getApplicationContext(), map, mClusterManager);
// Create selected custom Marker
RelativeLayout selectedContainer = (RelativeLayout) view.findViewById(R.id.marker_map_selected_container);
mSelectedItemView = ((LayoutInflater) getActivity().getSystemService
(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.map_marker_selected_layout, selectedContainer, false);
mSelectedIconGenerator.setContentView(mSelectedItemView);
mSelectedIconGenerator.setBackground(null);
// Create custom Marker
LinearLayout container = (LinearLayout) view.findViewById(R.id.text_marker_map_container);
mItemView = ((LayoutInflater) getActivity().getSystemService
(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.map_marker_layout, container, true);
mIconGenerator.setContentView(mItemView);
mIconGenerator.setBackground(null);
}
@Override
protected void onBeforeClusterItemRendered(VendorMapItem vendor, MarkerOptions markerOptions) {
// Draw a single vendor.
Bitmap icon;
if (null == selectedItem || !vendor.getPosition().equals(selectedItem.getPosition()))
{
icon = mIconGenerator.makeIcon();
} else {
icon = mSelectedIconGenerator.makeIcon();
}
markerOptions.title(vendor.getTitle());
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(icon));
}
@Override
protected void onBeforeClusterRendered(Cluster<VendorMapItem> cluster, MarkerOptions markerOptions) {
// Draw multiple vendors clustered...
Bitmap icon = mIconGenerator.makeIcon();
markerOptions.icon(BitmapDescriptorFactory.fromBitmap(icon));
}
@Override
protected boolean shouldRenderAsCluster(Cluster cluster) {
// Always render clusters.
return cluster.getSize() > 1;
}
}
通常会显示聚类项目,也会显示非聚类项目,无论是否选中。我有另一个视图显示列表中的所有位置数据,而我的远程位置只显示在那里。
知道我做错了什么吗?
public class ClusterIconProvider 实现 IconDataProvider {
private static final int[] res = {R.drawable.m1,R.drawable.m2,R.drawable.m3,R.drawable.m4,R.drawable.m5};
private static final int[] forCounts = { 10, 100, 1000, 10000, Integer.MAX_VALUE };
private Bitmap[] baseBitmaps;
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private Rect bounds = new Rect();
private MarkerOptions markerOptions = new MarkerOptions().anchor(0.5f, 0.5f);
public ClusterIconProvider(Resources resources){
baseBitmaps = new Bitmap[res.length];
for (int i = 0; i < res.length; i++) {
baseBitmaps[i] = BitmapFactory.decodeResource(resources, res[i]);
}
paint.setColor(Color.WHITE);
paint.setTextAlign(Align.CENTER);
paint.setTextSize(resources.getDimension(R.dimen.Cluster));
}
@Override
public MarkerOptions getIconData(int markersCount) {
// TODO Auto-generated method stub
Bitmap base;
int i = 0;
do {
base = baseBitmaps[i];
} while (markersCount >= forCounts[i++]);
Bitmap bitmap = base.copy(Config.ARGB_8888, true);
String text = String.valueOf(markersCount);
paint.getTextBounds(text, 0, text.length(), bounds);
float x = bitmap.getWidth() / 2.0f;
float y = (bitmap.getHeight() - bounds.height()) / 2.0f - bounds.top;
Canvas canvas = new Canvas(bitmap);
canvas.drawText(text, x, y, paint);
BitmapDescriptor icon = BitmapDescriptorFactory.fromBitmap(bitmap);
return markerOptions.icon(icon);
}
}
愚蠢。
基本上我调用 map.clear() 的次数太多了。我想如果地图上绘制的所有内容都是由聚类器控制的引脚,则无需调用 clear。一旦删除了 clear() 调用,我的图钉就会再次出现。