Shaheedah
Shaheedah

Reputation: 77

Can i add marker clustering to my already set map and markers?

so i already have a map set up and i have added all markers with icons and title using a function addMarkersToMap() but they are so sparse so i was thinking of clustering them. Is there a way for me to cluster the markers that i have already set up on my map? I found this code which is awesome for clustering from a .json file but my markers are already set up on the map and i don't know how to cluster markers with already set up icon and title.

           protected void startDemo() {
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(51.503186, -0.126446), 10));

        mClusterManager = new ClusterManager<MyItem>(this,map);

        map.setOnCameraChangeListener(mClusterManager);
        try {
            readItems();
        } catch (JSONException e) {
            Toast.makeText(this, "Problem reading list of markers.", Toast.LENGTH_LONG).show();
        }
    }

    private void readItems() throws JSONException {
        InputStream inputStream = getResources().openRawResource(R.raw.radar_search);
        List<MyItem> items = new MyItemReader().read(inputStream);
        for (int i = 0; i < 5; i++) {
            double offset = i / 60d;
            for (MyItem item : items) {
                LatLng position = item.getPosition();
                double lat = position.latitude + offset;
                double lng = position.longitude + offset;
                MyItem offsetItem = new MyItem(lat, lng);
                mClusterManager.addItem(offsetItem);
            }
        }

Upvotes: 2

Views: 1069

Answers (1)

RmK
RmK

Reputation: 1438

You cannot use cluster as well as your already setup custom marker (with Title and Snippet) together.

Once you start using cluster, all marker addition and related tasks will be taken care of by the clustermanager.

But all hope is not lost !!

You can add the POJO variables, their getters and setters inside the MarkerItem object you created for the cluster manager.

You can then create a class like this inside your map activity itself,

class OwnIconRendered extends DefaultClusterRenderer<MyItem> {

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

        @Override
        protected void onBeforeClusterItemRendered(MyItem item, MarkerOptions markerOptions) {
            //markerOptions.icon(item.getIcon());
            markerOptions.snippet(item.getContact());
            markerOptions.title(item.getName());
            super.onBeforeClusterItemRendered(item, markerOptions);
        }
}

and set this render for your clustermanager like this

mClusterManager.setRenderer(new OwnIconRendered(activity.getApplicationContext(), getMap(), mClusterManager));

For more detailed explanation you can refer this answer which I have referred. http://stackoverflow.com/questions/27745299/how-to-add-title-snippet-and-icon-to-clusteritem

Upvotes: 0

Related Questions