How to: Aggregate Vector Items Using a Clusterer

  • 2 minutes to read

This example shows how to cluster vector map items. Note that you can only cluster map items that implement the IClusterable interface (for example, MapDot, MapCallout, MapCustomElement and MapPushpin).

Assign an object of a class that implements the IMapDataAdapter.Clusterer interface to the IMapDataAdapter.Clusterer property. Then optionally, configure the clusterer. For example, all predefined clusters allow you to group items before clustering and allow you to customize the appearance of cluster representatives.

To group the items based on an attribute, assign AttributeGroupProvider to the MapClustererBase.GroupProvider property. Set the AttributeGroupProvider.AttributeName property to the item attribute name that should be used for clustering. After that, only items with the equal attribute value can be grouped into the same cluster.

Design a class that implements the IClusterItemFactory interface to customize the appearance of the cluster representatives. Then, call the MapClustererBase.SetClusterItemFactory method with an object of the class to assign the required factory object to the clusterer.

VectorItemsLayer VectorLayer { get { return (VectorItemsLayer)map.Layers["VectorLayer"]; } }
ListSourceDataAdapter DataAdapter { get { return (ListSourceDataAdapter)VectorLayer.Data; } }

private void Form1_Load(object sender, EventArgs e) {
    DataAdapter.DataSource = LoadData();
    DistanceBasedClusterer clusterer = new DistanceBasedClusterer {
        GroupProvider = new AttributeGroupProvider {
            AttributeName = "LocationName"
        }
    };
    clusterer.SetClusterItemFactory(new CustomClusterItemFactory());
    DataAdapter.Clusterer = clusterer;
}
class CustomClusterItemFactory : IClusterItemFactory {
    public MapItem CreateClusterItem(IList<MapItem> objects) {
        MapDot dot = new MapDot();
        dot.ClusteredItems = objects;
        dot.TitleOptions.Pattern = objects.Count.ToString();
        return dot;
    }
}