我正在使用android maps utils在google maps api v2上聚类标记。它运行良好,但当我添加2000+标记时,在最大缩放时它仍然是聚类的(标记仍有数字):
这是我用标记填充地图的方法:
public void getRiverData(String state, String type) {
URL = getResources().getString(R.string.base_url) + state + "/" + type
+ getResources().getString(R.string.end_url);
SimpleXmlRequest<XMLData> simpleRequest = new SimpleXmlRequest<XMLData>(
URL, XMLData.class, new Response.Listener<XMLData>() {
@Override
public void onResponse(XMLData response) {
// Initialize the manager with the context and the map.
// (Activity extends context, so we can pass 'this' in
// the constructor.)
mClusterManager = new ClusterManager<MarkerItem>(
getActivity(), map);
mClusterManager.setRenderer(new ClusterRenderer(
getActivity(), map, mClusterManager));
// response Object
waterItemsList = response.getNews();
for (News item : waterItemsList) {
if (item.getRiver_name() != null
&& item.getRiver_name() != "") {
water_level_value = item.getWater_level_value();
if (water_level_value != null
&& !water_level_value.equals(0)
&& !water_level_value.equals("")) {
MarkerItem offsetItem = new MarkerItem(item);
mClusterManager.addItem(offsetItem);
}
map.setOnMarkerClickListener(mClusterManager);
map.setInfoWindowAdapter(new InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker marker) {
return null;
}
@Override
public View getInfoContents(Marker marker) {
try {
View v = getActivity()
.getLayoutInflater()
.inflate(
R.layout.marker_info,
null);
TextView title = (TextView) v
.findViewById(R.id.tvMarkerTitle);
TextView info = (TextView) v
.findViewById(R.id.tvMarkerInfo);
title.setText(marker.getTitle()
.toString());
info.setText(marker.getSnippet()
.toString());
return v;
} catch (Exception e) {
// kliknięcie w cluster
return null;
}
}
});
}
}
map.setOnCameraChangeListener(mClusterManager);
map.setOnInfoWindowClickListener(mClusterManager);
mClusterManager.cluster();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// error Object
error.printStackTrace();
}
});
AppController.getInstance().addToRequestQueue(simpleRequest);
}
任何人都可以帮助我吗?为什么不起作用?
答案 0 :(得分:14)
您可以扩展DefaultClusterRenderer类并将最小标记设置为群集。
public class InfoMarkerRenderer extends DefaultClusterRenderer<MyCustomMarker> {
public InfoMarkerRenderer(Context context, GoogleMap map, ClusterManager<MyCustomMarker> clusterManager) {
super(context, map, clusterManager);
//constructor
}
@Override
protected void onBeforeClusterItemRendered(final MyCustomMarker infomarker, MarkerOptions markerOptions) {
// you can change marker options
}
@Override
protected boolean shouldRenderAsCluster(Cluster cluster) {
return cluster.getSize() > 5; // if markers <=5 then not clustering
}
}
答案 1 :(得分:1)
要过滤具有相同位置的标记,您可以简单地使用一个hashmasp,其键是根据标记坐标计算的。
类似的东西:
Map<String, Marker> uniqueMarkers = new HashMap<String, Marker>();
for (Markers m : allMarkers) {
// Compute a key to filter duplicates
// You may need to account for small floating point precision errors by
// rounding those coordinates
String key = m.getLatitude() + "|" + m.getLongitude();
if (uniqueMarkers.get(key)!=null ) {
// Skip if we have a marker with the same coordinates
continue;
}
// Add marker and do something with it
uniqueMarkers.add(key, m);
// ...
}
答案 2 :(得分:0)
通过跟踪和错误我发现如果标记在~10英尺内(相当于纬度或长度的0.0000350º差异),即使在最大缩放级别,标记也不会减少。
解决此问题的一种方法是实现自定义渲染器,让应用决定何时进行群集。例如,以下代码仅在有多个标记而非最大缩放时才会聚类。换句话说,它将以最大缩放比例去除所有标记。
mClusterManager.setRenderer(new DefaultClusterRenderer<MyItem>(mContext, googleMap, mClusterManager) {
@Override
protected boolean shouldRenderAsCluster(Cluster cluster) {
if(cluster.getSize() > 1 && mCurrentZoom < mMaxZoom) {
return true;
} else {
return false;
}
}
});