2016-08-16 90 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 /效用/標記物的聚類 –