放大Android Google Map群集时,第一个群集保持可见状态

时间:2016-09-17 17:59:27

标签: java android google-maps google-maps-markers cluster-computing

我设法为我的GMap实现了一个有效的集群管理器。但是,当我缩放时,第一个群集仍然可见。当我将算法设置为GridBaseAlgorithm时,我工作正常,但群集不在正确的位置。

以下是我的MapFragment上的代码:

private void setUpClusterer(GoogleMap mMap) {

    // Initialize the manager with the context and the map.
    // (Activity extends context, so we can pass 'this' in the constructor.)
    mClusterManager = new ClusterManager<>(context, mMap);

   // mClusterManager.setAlgorithm(new NonHierarchicalDistanceBasedAlgorithm<ToiletMarker>());

    // Point the map's listeners at the listeners implemented by the cluster
    // manager.
    mMap.setOnCameraIdleListener(mClusterManager);
    mMap.setOnMarkerClickListener(mClusterManager);

    mMap.setInfoWindowAdapter(mClusterManager.getMarkerManager());

    mMap.setOnInfoWindowClickListener(mClusterManager);
    mClusterManager.setOnClusterItemInfoWindowClickListener(this);

    mClusterManager.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<ToiletMarker>() {
        @Override
        public boolean onClusterItemClick(ToiletMarker item) {
            clickedClusterItem = item;
            return false;
        }
    });

    // Add cluster items (markers) to the cluster manager.
    addItems();

    mClusterManager.getMarkerCollection().setOnInfoWindowAdapter(
            new MyCustomAdapterForItems());
}

private void addItems() {
    for (Toilet toilet : toiletArrayList) {
        Location location = toilet.getLocation();
        double lat = location.getLatitude();
        double lon = location.getLongitude();

        ToiletMarker item = new ToiletMarker(lat, lon, toilet);
        mClusterManager.addItem(item);
    }
    mClusterManager.cluster();
}

@Override
public void onMapReady(GoogleMap googleMap) {
    Log.d(TAG, "Map ready");
    mMap = googleMap;

    setUpClusterer(mMap);
}

@Override
public void onClusterItemInfoWindowClick(ToiletMarker myItem) {
    Toast.makeText(context, "clicked on myItem : " + myItem.getPosition().toString(), Toast.LENGTH_SHORT).show();
}
public class MyCustomAdapterForItems implements GoogleMap.InfoWindowAdapter {

        @Override
        public View getInfoWindow(Marker marker) {
            return null;
        }

        @Override
        public View getInfoContents(Marker marker) {
            // Getting view from the layout file info_window_layout
            View v = LayoutInflater.from(context).inflate(R.layout.markerlayout, null);

            mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
                public void onInfoWindowClick(Marker marker) {
                    LatLng latLng = marker.getPosition();
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?daddr=" + latLng.latitude + "," + latLng.longitude));
                    context.startActivity(intent);
                }
            });

            //Do stuff
        // Returning the view containing InfoWindow contents
        return v;
    }

这是2张图片: First cluser 200+

Zoomed, first cluster 200+ always here !

编辑:

花了一个小时试图解决后,我终于得到了什么:问题是集群管理器工作正常,问题是填充地图的方法被调用了两次。

要解决此问题,只需添加mMap.clear();在添加标记之前:)

2 个答案:

答案 0 :(得分:0)

Here,您可以查看我的自定义ClusterRenderer并尝试实现您的修改和需求。

主要是:

override fun shouldRenderAsCluster(cluster: Cluster<MapClusterItem>?): Boolean {
       val isZoomLevelSuites = currentZoomLevel < MAX_ZOOM_LVL - POINT_ZOOM_LVL
       return super.shouldRenderAsCluster(cluster) && isZoomLevelSuites
}

在您指定的某个缩放级别之后,它不应渲染群集。希望对您有所帮助!

答案 1 :(得分:0)

就我而言,我同时添加了地图和clusterManager的标记。 如果您将项目添加到clusterManager-您已经在地图上有了标记。