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

问题描述 投票:2回答:1

我设法为我的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();在添加标记之前:)

java android google-maps google-maps-markers cluster-computing
1个回答
0
投票

Here您可以查看我的自定义ClusterRenderer并尝试实施您的编辑和需求。

主要是:

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

在您指定的某种缩放级别后,它不应呈现群集。希望它会有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.