我对谷歌地图中的自定义标记有疑问。我会尽力解释。
我有一些标记,我做了一个 Asyntask,如果有必要的话,它会聚类,返回一个LinkedHashMap<Point, ArrayList<MarkerOptions>> clusters
我有集群的地方。每个位置代表一个簇(可以有一个带有 1 个标记的簇)
当我得到这个列表时,我将集群添加到地图中:
这是我调用 Clusterizer 的活动。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
map.getUiSettings().setMyLocationButtonEnabled(true);
CameraUpdate camUpd1 = CameraUpdateFactory.newLatLngZoom(new LatLng(41.40520680710329,2.191342011603923),MAP_ZOOM_LEVEL);
map.animateCamera(camUpd1);
loadMarkers();
map.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition position) {
if(position.zoom != oldZoom) {
try {
clusters = null;
map.clear();
Clusterizer.setContext(getApplicationContext());
clusters = Clusterizer.clusterMarkers(map, markers, INTERVAL);
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
oldZoom = position.zoom;
}
});
}
private void loadMarkers() {
markers.add(new Marker(41.40520680710229,2.191342011603823,"Glorias1","Centro Comercial",true).getMarker());
markers.add(new Marker(41.40520680710229+0.0005,2.191342011603823-0.0005,"Glorias11","Centro Comercial",true).getMarker());
markers.add(new Marker(41.40520680710229+0.0005,2.191342011603823+0.0005,"Glorias111","Centro Comercial",true).getMarker());
markers.add(new Marker(41.40520680710229-0.0005,2.191342011603823+0.0005,"Glorias1111","Centro Comercial",true).getMarker());
markers.add(new Marker(41.40520680710229-0.0005,2.191342011603823-0.0005,"Glorias1111","Centro Comercial",true).getMarker());
}
并且,在 Asyntask 计算集群之后(它确实可以)postExecute 方法,这样做:
@Override
protected void onPostExecute(
LinkedHashMap<Point, ArrayList<MarkerOptions>> clusters) {
map.clear();
for(Point point: clusters.keySet()) {
ArrayList<MarkerOptions> markersForPoint = clusters.get(point);
MarkerOptions mainMaker= markersForPoint.get(0);
//If the point (taken from cluster, has more than 1 markerOption, means that has been clusterized, so It have to be printed with the //modified canvas that contains the number of markers clusterized)
if(markersForPoint.size() > 1) {
mainMaker.title(Integer.toString(markersForPoint.size()));
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
Bitmap bmp = Bitmap.createBitmap(80, 80, conf);
Canvas canvas1 = new Canvas(bmp);
Paint color = new Paint();
color.setTextSize(35);
color.setColor(Color.BLACK
canvas1.drawBitmap(BitmapFactory.decodeResource(context.getResources(),
R.drawable.pin), 0,0, color);
canvas1.drawText("",10,40,color);
canvas1.drawText(Integer.toString(markersForPoint.size()), 10, 40, color);
mainMaker.icon(BitmapDescriptorFactory.fromBitmap(bmp));
mainMaker.anchor(0.5f, 1);
}
map.addMarker(mainMaker);
}
}
这就是问题所在...
如果它是聚集的,没有问题,它显示正常,但是当我放大它时,cluster list
它是好的,但标记不是
那是什么时候集群看起来是“集群的”,什么时候不应该集群
我该如何解决?我在添加标记之前清理了地图......
谢谢大家!
集群:
未聚集