【问题标题】:Google maps android on cluster items click not working谷歌在集群项目上映射 android 点击不工作
【发布时间】:2016-08-16 02:54:11
【问题描述】:

在我的谷歌地图片段中, 我用它来将我的项目添加为集群

mClusterManager = new ClusterManager<ContactInfo>(getActivity(), googleMap);
mClusterManager.setOnClusterClickListener(this);
mClusterManager.addItem(myItem);

现在,我可以通过

设置 onClusterClickListener
mClusterManager.setOnClusterClickListener(this);

通过使用上面的代码,我可以检测到我什么时候点击了这些集群, 但是,当我单击这些单独的标记时,这不起作用。

如何检测我添加到 clusterManager 的那些单独的标记?

【问题讨论】:

  • 我的建议是不要使用“Android Marker Clustering Utility”,而是使用“android map extension”,因为默认库无法处理 1000 个标记。

标签: android google-maps google-maps-api-3


【解决方案1】:

setOnClusterClickListener 在集群被点击时被调用。

您还需要设置setOnClusterItemClickListener,即

设置在单个 ClusterItem 被调用时调用的回调 轻拍。注意:要使此侦听器起作用,ClusterManager 必须 作为点击监听器添加到地图中。

并且一定要实现ClusterManager.OnClusterItemClickListener&lt;T extends ClusterItem&gt;

【讨论】:

  • 谢谢,看来我还需要添加googleMap.setOnMarkerClickListener(mClusterManager);a
  • 对,忘了说
【解决方案2】:

试试这个自定义类。

private class PersonRenderer extends DefaultClusterRenderer<Person> {

        public PersonRenderer() {
            super(MainActivity.this, mMap, mClusterManager1);

        }

        @Override
        protected void onBeforeClusterItemRendered(Person person, MarkerOptions markerOptions) {
            Debug.e("call", "onBeforeClusterItemRendered");
            // Draw a single person.
            // Set the info window to show their name.


        }


        @Override
        protected boolean shouldRenderAsCluster(Cluster<Person> cluster) {
            Debug.e("call", "shouldRenderAsCluster");
            return cluster.getSize() > 1;
        }

        @Override
        protected void onClusterItemRendered(Person clusterItem, Marker marker) {
            super.onClusterItemRendered(clusterItem, marker);
            Debug.e("call", "onClusterItemRendered");

        }
    }

onClusterClick

  @Override
    public boolean onClusterClick(Cluster<Person> cluster) {
        Debug.e("call", "onClusterClick");

        // Show a toast with some info when the cluster is clicked.
        String firstName = cluster.getItems().iterator().next().name;
//        Toast.makeText(this, cluster.getSize() + " (including " + firstName + ")", Toast.LENGTH_SHORT).show();

        // Zoom in the cluster. Need to create LatLngBounds and including all the cluster items
        // inside of bounds, then animate to center of the bounds.

        // Create the builder to collect all essential cluster items for the bounds.
        LatLngBounds.Builder builder = LatLngBounds.builder();
        for (ClusterItem item : cluster.getItems()) {
            builder.include(item.getPosition());
        }
        // Get the LatLngBounds
        final LatLngBounds bounds = builder.build();



        // Animate camera to the bounds
        try {
            mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 100));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return true;
    }

onClusterInfoWindowClick

 @Override
    public void onClusterInfoWindowClick(Cluster<Person> cluster) {
        Debug.e("call", "onClusterInfoWindowClick");
        // Does nothing, but you could go to a list of the users.
//        clickedCluster = cluster;
    }

onClusterItemClick

  @Override
    public boolean onClusterItemClick(Person item) {
        // Does nothing, but you could go into the user's profile page, for example.
        Debug.e("call", "onClusterItemClick");

        clickedClusterItem = item;

        return false;
    }

onClusterItemInfoWindowClick

  @Override
    public void onClusterItemInfoWindowClick(Person item) {
        // Does nothing, but you could go into the user's profile page, for example.
        Debug.e("call", "onClusterItemInfoWindowClick");

    }

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2016-11-21
  • 1970-01-01
  • 2013-12-13
  • 2012-06-11
  • 2013-04-24
  • 2021-11-10
  • 2016-03-27
  • 1970-01-01
相关资源
最近更新 更多