Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cluster Marker not hiding when zoom in

I'm trying to implement a cluster marker on my map, and it is behaving a little strange, first, it shows me the cluster marker but also shows me the point markers, that shouldn't be happening, and when I zoom in the cluster marker still showing, i'll add some images to explain it better.

enter image description here

enter image description here

public class MapaViagem extends FragmentActivity {

    private GoogleMap googleMap;
    private String rm_IdViagem;
    private List<ClienteModel> mClienteModel = new ArrayList<ClienteModel>();
    private List<EnderecoModel> mEnderecoModel = new ArrayList<EnderecoModel>();
    private ViagemModel mViagemModel = new ViagemModel();
    private ClusterManager<MyItem> mClusterManager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.maps);
         ArrayList<LatLng> coordList = new ArrayList<LatLng>();

        try {

            Bundle parametros = getIntent().getExtras();
            rm_IdViagem = parametros.getString("id_viagem");

            Repositorio ca = new Repositorio(this);
            mViagemModel = ca.getViagemPorId(Integer.valueOf(rm_IdViagem));

            Repositorio cl = new Repositorio(this);
            mClienteModel = cl.getClientesViagem(Integer.valueOf(rm_IdViagem));


            System.out.println("TEM CLIENTE " + mClienteModel.size());

            if(mClienteModel != null) {

                for (int i = 0; i < mClienteModel.size(); i++) {


                    Repositorio mRepositorio = new Repositorio(this);
                    mEnderecoModel = mRepositorio.getListaEnderecosDoCliente(Integer.valueOf(mClienteModel.get(i).getClientes_id()));


                    for (int j = 0; j < mEnderecoModel.size(); j++) {
                        // Loading map
                        initilizeMap();
                        // Changing map type
                        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

                        // Showing / hiding your current location
                        googleMap.setMyLocationEnabled(true);

                        // Enable / Disable zooming controls
                        googleMap.getUiSettings().setZoomControlsEnabled(true);

                        // Enable / Disable my location button
                        googleMap.getUiSettings().setMyLocationButtonEnabled(true);

                        // Enable / Disable Compass icon
                        googleMap.getUiSettings().setCompassEnabled(true);

                        // Enable / Disable Rotate gesture
                        googleMap.getUiSettings().setRotateGesturesEnabled(true);

                        // Enable / Disable zooming functionality
                        googleMap.getUiSettings().setZoomGesturesEnabled(true);


                        final float latitude = Float.parseFloat(mEnderecoModel.get(j).getLatitude());
                        final float longitude = Float.parseFloat(mEnderecoModel.get(j).getLongitude());


                        coordList.add(new LatLng(latitude, longitude));


                        googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), 10));



                        mClusterManager = new ClusterManager<MyItem>(MapaViagem.this, googleMap);


                        mClusterManager.setRenderer(new MyClusterRenderer(MapaViagem.this, googleMap, mClusterManager));

                        googleMap.setOnCameraChangeListener(mClusterManager);
                        googleMap.setOnMarkerClickListener(mClusterManager);


                        addItems(coordList);

                    }


                }
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public class MyClusterRenderer extends DefaultClusterRenderer<MyItem> {

        public MyClusterRenderer(Context context, GoogleMap map,
                                 ClusterManager<MyItem> clusterManager) {
            super(context, map, clusterManager);
        }

        @Override
        protected void onBeforeClusterItemRendered(MyItem item, MarkerOptions markerOptions) {
            super.onBeforeClusterItemRendered(item, markerOptions);



//            item.setTitle(mClienteModel.get(i));
            markerOptions.title(item.getTitle());
        }

        @Override
        protected void onClusterItemRendered(MyItem clusterItem, Marker marker) {
            super.onClusterItemRendered(clusterItem, marker);

            //here you have access to the marker itself
        }

 @Override
        protected boolean shouldRenderAsCluster(Cluster<MyItem> cluster) {
            return cluster.getSize() > 3; // when count of markers is more than 3, render as cluster
        }
    }


    private void addItems(List<LatLng> markers) {

        for (int i = 0; i < markers.size(); i++) {
            MyItem offsetItem = new MyItem(markers.get(i));
            mClusterManager.addItem(offsetItem);

        }
        mClusterManager.cluster();
    }



    private void initilizeMap() {
        if (googleMap == null) {
            googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                    R.id.map)).getMap();

            // check if map is created successfully or not
            if (googleMap == null) {
                Toast.makeText(getApplicationContext(),
                        "Não foi possível carregar o mapa", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        initilizeMap();
    }


}

Model:

public class MyItem implements ClusterItem {

    private LatLng mPosition;
    private String title;

    public MyItem(LatLng position){
        mPosition = position;
    }

    @Override
    public LatLng getPosition() {
        return mPosition;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}
like image 974
Stack two Avatar asked Dec 01 '14 19:12

Stack two


People also ask

What happens when you zoom in and out of a cluster?

Notice that as you zoom into any of the cluster locations, the number on the cluster decreases, and you begin to see the individual markers on the map. Zooming out of the map consolidates the markers into clusters again.

How does marker clustering work?

To see how marker clustering works, view the map below. The number on a cluster indicates how many markers it contains. Notice that as you zoom into any of the cluster locations, the number on the cluster decreases, and you begin to see the individual markers on the map.

How do I combine markers of close proximity into clusters?

You can use the @googlemaps/markerclusterer library in combination with the Maps JavaScript API to combine markers of close proximity into clusters, and simplify the display of markers on the map. To see how marker clustering works, view the map below. The number on a cluster indicates how many markers it contains.

What is marker clustering in OpenCV?

Understanding marker clustering. The MarkerClusterer library uses the grid-based clustering technique that divides the map into squares of a certain size (the size changes at each zoom level), and groups the markers into each square grid. It creates a cluster at a particular marker, and adds markers that are in its bounds to the cluster.


3 Answers

Firstly, you need to define, when your ClusterManager should clusterize, so there is a method in MyClusterRenderer you should override:

@Override
protected boolean shouldRenderAsCluster(Cluster<MyItem> cluster) {
    return cluster.getSize() > 3; // when count of markers is more than 3, render as cluster
}

Secondly, when you make some changes with your markers such as adding, removing, changing title, icon, location, you should call cluster() method. So your addItems method should look like this:

private void addItems(List<LatLng> markers) {

    for (int i = 0; i < markers.size(); i++) {
        MyItem offsetItem = new MyItem(markers.get(i));
        mClusterManager.addItem(offsetItem);
    }
    mClusterManager.cluster();
}

EDIT:

I just look at your code one more time, and found that you forgot to set the clusterizing algorithm, it should be:

    ...
    mClusterManager = new ClusterManager<MyItem>(MapaViagem.this, googleMap);
    mClusterManager.setAlgorithm(new GridBasedAlgorithm<MyItem>());

    mClusterManager.setRenderer(new MyClusterRenderer(MapaViagem.this, googleMap, mClusterManager));
    ...
like image 70
romtsn Avatar answered Oct 24 '22 08:10

romtsn


I know that this is an old post, but today I had the same issue.

After spend hour trying to solve, I finally got what happen with my application: The thing is that the cluster manager is working correctly, the problem is that my method which populates the map was being called twice.

To solve this issue just add mMap.clear(); before add markers. Works here like a charm!

Bye.

like image 26
phtoxa Avatar answered Oct 24 '22 08:10

phtoxa


I had this issue and solved it by making sure that the ClusterManager was only being created and assigned once to the map, try putting and if statement in your create method. Most likely when you are initialising the map again in onResume that is causing the issue.

    public void onMapReady(GoogleMap googleMap) {
    map = googleMap;

    //only create one instance of cluster manager, 
    // if created again in onResume then click listeners don't work on reload and 
    //some markers don't disappear correctly on Zoom
    if (clusterManager == null){
    clusterManager =new ClusterManager(this, map);
     }

    map.setOnMarkerClickListener(clusterManager);
    map.setOnInfoWindowClickListener(clusterManager.getMarkerManager());
    map.setBuildingsEnabled(true);
    //noinspection MissingPermission
    map.setMyLocationEnabled(true);
    map.setOnCameraIdleListener(clusterManager);


    clusterManager.setOnClusterItemInfoWindowClickListener(this);


    clusterManager.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<MyItem>() {
                @Override
                public boolean onClusterItemClick(MyItem item) {
                    return false;
                }
            });
like image 26
dwkstuart Avatar answered Oct 24 '22 10:10

dwkstuart