我是新来的,所以欢迎任何反馈。我正在尝试使用DefaultClusterRenderer来显示一些自定义群集,并且我试图在地图完全放大时不让它聚集。所以我找到了完全相同问题的答案: Disable clustering at max zoom level with Googles android-maps-utils 所以我尝试了那段代码,但我得到的是 Not on the main thread 错误。任何帮助将不胜感激。 我在构造函数中有这个
public EspecieRenderer(Context context, GoogleMap map, ClusterManager<T> clusterManager) {
super(context, map, clusterManager);
this.mMap = map;
...
}
然后我正在做
@Override
protected boolean shouldRenderAsCluster(Cluster<T> cluster) {
final float currentZoom = mMap.getCameraPosition().zoom;
final float currentMaxZoom = mMap.getMaxZoomLevel();
return currentZoom < currentMaxZoom && cluster.getSize() >= 10;
}
以下是我正在初始化群集的方法。我正在使用线程,因为我有近200种物种要加载,每个都有一个或多个标记要添加到地图
private void setUpClusterer() {
clusterManager = new ClusterManager<>(this, googleMap);
clusterManager.setRenderer(new EspecieRenderer<>(this, googleMap, clusterManager));
googleMap.setOnCameraChangeListener(clusterManager);
googleMap.setOnMarkerClickListener(clusterManager);
ArrayList<Especie> especies = (ArrayList<Especie>) Especie.list(this);
for (Especie especie : especies) {
ExecutorService queue = Executors.newSingleThreadExecutor();
queue.execute(new EspecieLoader(this, especie));
}
}
EspecieLoader
public class EspecieLoader implements Runnable {
private MainActivity context;
private Especie especie;
public EspecieLoader(MainActivity mainActivity, Especie especie) {
this.context = mainActivity;
this.especie = especie;
}
@Override
public void run() {
List<Foto> fotos = Foto.findAllByEspecieWithdCoords(context, especie);
String nombre = especie.getNombreCientifico();
for (Foto foto : fotos) {
LatLng latLng = new LatLng(foto.getLatitud(), foto.getLongitud());
Bitmap bitmap;
String path = "new/" + foto.getPath().replaceAll("-", "_").toLowerCase();
try {
bitmap = ResourcesHelper.getEncyclopediaAssetByName(context, path);
EspecieMarker especieMarker = new EspecieMarker(nombre, bitmap, latLng);
context.addSpeciesMarker(especieMarker);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
当我尝试运行我的应用时,我得到了这个:
07-16 19:18:41.402 31151-32075/com.lzm.Cajas E/AndroidRuntime: FATAL EXCEPTION: Thread-47332
Process: com.lzm.Cajas, PID: 31151
java.lang.IllegalStateException: Not on the main thread
2at maps.f.g.b(Unknown Source)
at maps.z.F.a(Unknown Source)
at maps.af.t.a(Unknown Source)
at vl.onTransact(:com.google.android.gms.DynamiteModulesB:51)
at android.os.Binder.transact(Binder.java:387)
at com.google.android.gms.maps.internal.IGoogleMapDelegate$zza$zza.getCameraPosition(Unknown Source)
at com.google.android.gms.maps.GoogleMap.getCameraPosition(Unknown Source)
at com.lzm.Cajas.map.EspecieRenderer.shouldRenderAsCluster(EspecieRenderer.java:62)
at com.google.maps.android.clustering.view.DefaultClusterRenderer$RenderTask.run(DefaultClusterRenderer.java:416)
at java.lang.Thread.run(Thread.java:818)
答案 0 :(得分:11)
我今天遇到了同样的需求。这是我的解决方案: 由于您无法设置setOnCameraChangeListener两次,请删除
googleMap.setOnCameraChangeListener(clusterManager);
并将其替换为
googleMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition cameraPosition) {
shouldCluster_zoom = cameraPosition.zoom < 9; //disables the cluster at 9 and higher zoom levels
clusterManager.onCameraChange(cameraPosition); //Replaces googleMap.setOnCameraChangeListener(clusterManager);
}
});
“shouldCluster_zoom
”显然是一个全局变量,静态和布尔值。
现在在“shouldRenderAsCluster
”方法检查中,根据缩放,是否应该聚类标记:
@Override
protected boolean shouldRenderAsCluster(Cluster<T> cluster) {
return super.shouldRenderAsCluster(cluster) && Your_class_name.shouldCluster_zoom;
}
答案 1 :(得分:0)
在@ Alberto97的答案之上进行编辑:
现在不建议使用OnCameraChange方法。他们用三个摄像头监听器替换了GoogleMap.OnCameraChangeListener():
我会使用OnCameraIdleListener
googleMap.setOnCameraIdleListener(new GoogleMap.OnCameraIdleListener() {
@Override
public void onCameraIdle() {
shouldCluster_zoom = cameraPosition.zoom < 9;
clusterManager.onCameraIdle();
}
});