2016-08-16 91 views
2

在我的谷歌地图片段, 我用这个来我的项目添加为集群现在谷歌地图Android版集群项目,请点击不工作

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

,我可以管理

mClusterManager.setOnClusterClickListener(this); 
设置onClusterClickListener

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

如何检测我添加到clusterManager中的那些分离标记?

+0

我的建议是不要使用“Android的标记集群工具”来代替使用“Android的地图扩展”事业默认库不能处理1000个标记。 – Rahul

回答

2

setOnClusterClickListener在点击群集时被调用。

您还需要设置setOnClusterItemClickListener这是

设置当一个人ClusterItem是 挖掘其所调用的回调。注意:为使此监听器正常工作,ClusterManager必须将 作为点击监听器添加到地图中。

而且一定要实现ClusterManager.OnClusterItemClickListener<T extends ClusterItem>

+2

谢谢,似乎我还需要添加googleMap.setOnMarkerClickListener(mClusterManager);一个 – Weijia

+0

是的,忘了提及 – Sohaib

0

尝试这个自定义类。

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"); 

    } 
+0

这很容易google google样本https://developers.google.com/maps/documentation/android-api /效用/标记物的聚类 –