使用Googleogles android-maps-utils禁用最大缩放级别的聚类 [英] Disable clustering at max zoom level with Googles android-maps-utils

查看:233
本文介绍了使用Googleogles android-maps-utils禁用最大缩放级别的聚类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 android-maps-utils中的群集在Android中的Google地图上聚集标记。我想在地图处于最大缩放级别时禁用聚类(即,如果地图处于最大缩放级别,总是渲染单个ClusterItems,否则按正常情况聚簇)。

通过在扩展DefaultClusterRenderer的自定义类中的shouldRenderAsCluster()中对缩放级别进行测试,我几乎可以实现它的工作。但是,我的解决方案滞后于实际缩放级别一步。例如,如果地图的最大缩放级别为21级,并且用户从级别20放大到级别21,则shouldRenderAsCluster中的检查将获得当前缩放级别20.如果用户缩小到级别20,则该检查将会得到21级。ClusterItems被渲染为单个项目,就像我想要的那样,但是一个缩放操作太晚了。



我得到当前缩放(当前不是最新的显然)来自DefaultClusterRenderer中设置的变量mZoom。



这是代码:

  public class UserClusterRenderer extends PosMapDefaultClusterRenderer< UserClusterItem> {

// ...

@Override
protected boolean shouldRenderAsCluster(Cluster cluster){
return mZoom< mMap.getMaxZoomLevel()&& cluster.getSize()> 1;




$ b

变量mZoom和mMap在DefaultClusterRenderer中设置。因为他们在那里有私人访问权限,所以我创建了一个名为PosMapDefaultClusterRenderer的DefaultClusterRenderer的副本。唯一的区别是mZoom和mMap被声明为protected,所以它们可以在UserClusterRenderer中访问。



为什么mZoom滞后一个缩放操作?有没有一种方法可以获得真正的缩放级别,而这些缩放级别正在渲染集群?



缩放是通过使用cameraPosition中的cameraUpdate调用animateCamera完成的。 / p>

这也是我在StackOverflow上的第一个问题,因此欢迎任何关于标签,格式等的输入。

编辑: Vai的答案之后的工作代码:

  public class UserClusterRenderer extends DefaultClusterRenderer< UserClusterItem> {

GoogleMap mMapCopy; //在构造函数中存储ref到映射

... ...
$ b @Override
protected boolean shouldRenderAsCluster(Cluster cluster){
float currentZoom = mMapCopy.getCameraPosition()。zoom;
float currentMaxZoom = mMapCopy.getMaxZoomLevel();
return currentZoom< currentMaxZoom&& cluster.getSize()> 1;



$ div $解析方案

你不需要复制整个类来获得 mZoom 。您可以使用以下方式获得当前的缩放比例(不会滞后,我希望):

  mMap.getCameraPosition()。zoom 

您的 shouldRenderAsCluster()方法看起来正确。让我知道如果这不起作用,我们会检查出来。欢迎来到SO。


I'm clustering markers on a Google map in Android using the clustering from android-maps-utils. I want to disable the clustering when the map is at it's max zoom level (ie always render individual ClusterItems if the map is at max zoom level, otherwise cluster as normal).

I'm able to almost get it working by doing a test for zoom level in shouldRenderAsCluster() in a custom class extending DefaultClusterRenderer. However my solution lags one step behind the actual zoom level. For instance if the max zoom for the map is level 21, and the user zooms in from level 20 to level 21, the check in shouldRenderAsCluster will get a current zoom level of 20. If the user then zooms out to level 20 the check will get level 21. The ClusterItems gets rendered as individual items like I want, but one zoom action too late.

I get the "Current Zoom" (which is not really current apparently) from the variable mZoom that is set in DefaultClusterRenderer.

This is the code:

public class UserClusterRenderer extends PosMapDefaultClusterRenderer<UserClusterItem> {

    // ...

    @Override
    protected boolean shouldRenderAsCluster(Cluster cluster) {
        return mZoom < mMap.getMaxZoomLevel() && cluster.getSize() > 1;
    }
}

The variables mZoom and mMap are set in DefaultClusterRenderer. Because they have private access there I've made a copy of DefaultClusterRenderer called PosMapDefaultClusterRenderer. The only difference is that mZoom and mMap are declared protected instead so they're accessible in UserClusterRenderer.

Why is mZoom lagging one zoom action behind? Is there a way to get the real zoom level for which the clusters are being rendered?

The zooming is done with a call to animateCamera using a cameraUpdate from a cameraPosition.

Also this is my first question on StackOverflow so any input on tags, formatting etc is welcome.

Edit: Working code after Vai's answer:

public class UserClusterRenderer extends DefaultClusterRenderer<UserClusterItem> {

    GoogleMap mMapCopy; // Store a ref to the map here from constructor

    // ...

    @Override
    protected boolean shouldRenderAsCluster(Cluster cluster) {
        float currentZoom = mMapCopy.getCameraPosition().zoom;
        float currentMaxZoom = mMapCopy.getMaxZoomLevel();
        return currentZoom < currentMaxZoom && cluster.getSize() > 1;
    }
}

解决方案

You don't need to copypaste the whole class to get mZoom. You can get the current zoom (not lagged, I hope) using:

mMap.getCameraPosition().zoom

Your approach of shouldRenderAsCluster() looks correct. Let me know if this doesn't work and we'll check it out. Welcome to SO.

这篇关于使用Googleogles android-maps-utils禁用最大缩放级别的聚类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆