One of the cool things about map applications in Silverlight is that they don't have the same limits on the amount of overlays it can display as HTML based maps have. This means that you can easily display 1000+ overlays without any noticeable degradation in performance. But just because your Silverlight map can handle it doesn't mean that your users can maintain their overview if you flood them with markers. This is where clustering comes. A cluster allows you to display a marker to represent a bunch of others, and when the cluster marker is clicked the contained markers are displayed.
The OverlayManager now supports displaying overlays in clusters. The OverlayManager's Clustering property takes an ICluster interface. The ICluster interface defines the values needed for the OverlayManager to cluster overlays in the collection. As long as your class implements the ICluster interface you can now define your clustering rule to fit your own application. The strength of the Silverlight UI engine and the flexibility gained from attached properties mean that you have total control of your clustering. However with total control come the problems of implementation.
The Reimers.Silverlight assembly comes with one clustering rule defined, the BoundsClustering class. This class one way of clustering overlays that made sense to me. Use it if it makes sense to you, or use it as an example for your own clustering. The clustering is defined as follows:
- It receives a collection of overlays and figures out what the bounding box of the collection is.
- Inside this bounding box it calculates the base location of the desired number of clusters.
- It runs through the collection again and adds each overlay to the nearest base location.
- Each base location is moved to the average location of the associated overlays.
- It runs through each updated base location and places a marker at that location
- Each marker gets the contained overlays associated as an attached property.
The OverlayManager doesn't treat cluster markers as regular markers, but keeps them separated. When they are clicked the OverlayManager reads the ContainedOverlays attached property and displays the associated markers. If you are using some other way to store the markers (ex a WCF call) then you need to handle either the ClusterOpening or ClusterOpened event of the OverlayManager.
You can define the clustering directly in XAML directly in XAML like so:
XAML
<UserControl x:Class="MapMap.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:map="clr-namespace:Microsoft.VirtualEarth.MapControl;assembly=Microsoft.VirtualEarth.MapControl" xmlns:ve="clr-namespace:Reimers.Silverlight.VirtualEarth;assembly=Reimers.Silverlight"> <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> <map:Map x:Name="map"> <ve:OverlayManager.Manager> <ve:OverlayManager> <ve:OverlayManager.Clustering> <ve:BoundsClustering DesiredClusterCount="9" /> </ve:OverlayManager.Clustering> </ve:OverlayManager> </ve:OverlayManager.Manager> </map:Map> </Grid> </UserControl>
One thing to note, clustering is not performed automatically. You must call the ClusterOverlays method on the OverlayManager. This is done to give you the flexibilty to define when to activate the clustering.


