将多个ClusterManager添加到Google地图

时间:2015-04-29 06:56:48

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

我正在尝试使用两个ClusterManager for Google Map,但我只能添加一个clustermanager和它的项目点击事件,

googleMap.setOnMarkerClickListener(mClusterManager);

mClusterManager.setOnClusterItemClickListener(new OnClusterItemClickListener<Person>() {

    @Override
    public boolean onClusterItemClick(Person item) {
        Log.v("Cluster Click", "Item Click:"+item.getName());
        return true;
    }
});

如何向googlemap添加一个群集管理器?

提前致谢。

3 个答案:

答案 0 :(得分:10)

是的,将点击监听器委托给两个集群管理器。

map.setOnClickListener(new GoogleMap.OnMarkerClickListener() {
  boolean onMarkerClick(Marker m) {
    clusterManager1.onMarkerClick(m)
    clusterManager2.onMarkerClick(m)
  }
});

答案 1 :(得分:8)

您可以像这样使用MarketManager:

MarkerManager markerManager = new MarkerManager(mMap);

mClusterManager1 = new ClusterManager<>(getContext(), mMap,markerManager);
mClusterManager2 = new ClusterManager<>(getContext(), mMap,markerManager);

mMap.setOnMarkerClickListener(markerManager);

mMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
        @Override
        public void onCameraChange(CameraPosition cameraPosition) {
            mClusterManager1.onCameraChange(cameraPosition);
            mClusterManager2.onCameraChange(cameraPosition);
        }
    });


mClusterManager1.setRenderer(myClusterRenderer1 = new MyClusterRenderer(getContext(), mMap, mClusterManager1));
mClusterManager2.setRenderer(myClusterRenderer2 = new MyClusterRenderer(getContext(), mMap, mClusterManager2));

答案 2 :(得分:0)

<块引用>
Here is how I did it: 
KOTLIN version
override fun onMapReady(googleMap: GoogleMap?) {
 this.googleMap = googleMap

    //Step 1
    
        val markerManager = MarkerManager(googleMap) // your googleMap instance
    
     
    //Step 2


    mEquipmentClusterManager = ClusterManager(activity, googleMap,  markerManager)
    mCustomerClusterManager = ClusterManager(activity, googleMap, markerManager)

    googleMap.setOnMarkerClickListener(markerManager)
    googleMap.setOnCameraIdleListener(this)

    //Step 3
    
    val rendererCustomer =CustomerAroundMeMapPinsCluster(activity,googleMap, mCustomerClusterManager)
    
    val rendererEquipmentRental = EquipmentAroundMeMapPinsCluster(activity,googleMap,mEquipmentClusterManager)

    //Step 4
    
       

     mCustomerClusterManager.setOnClusterClickListener {
                    Toast.makeText(context, "Customer Cluster click", Toast.LENGTH_SHORT).show()
                    onClusterClick(it)
                    return@setOnClusterClickListener false
                }
    mCustomerClusterManager.setOnClusterItemClickListener {
                Toast.makeText(context, "Customer item click", Toast.LENGTH_SHORT).show()
                
                return@setOnClusterItemClickListener false
            }

    mEquipmentClusterManager.setOnClusterClickListener {
            Toast.makeText(context, "Equipment Cluster click", Toast.LENGTH_SHORT).show()
            return@setOnClusterClickListener true
        }
        mEquipmentClusterManager.setOnClusterItemClickListener {
            // there is a ticket for this
            Toast.makeText(context, "Equipment item clicked", Toast.LENGTH_SHORT).show()
            
            return@setOnClusterItemClickListener false
        }
 

    mCustomerClusterManager.renderer = rendererCustomer
    mEquipmentClusterManager.renderer = rendererEquipmentRental


}


     override fun onCameraIdle() {
            mEquipmentClusterManager.cluster()
            mCustomerClusterManager.cluster()
    
        }