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 (see MapDot), MapCallout (see MapCallout), MapCustomElement (see MapCustomElement) and MapPushpin (see 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.
Form1.cs |
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;
}
}
|
Form1.vb |
Private ReadOnly Property VectorLayer() As VectorItemsLayer
Get
Return CType(map.Layers("VectorLayer"), VectorItemsLayer)
End Get
End Property
Private ReadOnly Property DataAdapter() As ListSourceDataAdapter
Get
Return CType(VectorLayer.Data, ListSourceDataAdapter)
End Get
End Property
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
DataAdapter.DataSource = LoadData()
Dim clusterer As DistanceBasedClusterer = New DistanceBasedClusterer With {
.GroupProvider = New AttributeGroupProvider With {.AttributeName = "LocationName"}
}
clusterer.SetClusterItemFactory(New CustomClusterItemFactory())
DataAdapter.Clusterer = clusterer
End Sub
Friend Class CustomClusterItemFactory
Implements IClusterItemFactory
Private Function CreateClusterItem(objects As IList(Of MapItem)) As MapItem Implements IClusterItemFactory.CreateClusterItem
Dim dot As New MapDot()
dot.ClusteredItems = objects
dot.TitleOptions.Pattern = objects.Count.ToString()
Return dot
End Function
End Class
|