Select to view content in your preferred language

Clustering off/on

2004
6
07-28-2011 05:28 AM
gabrielvazquez
Deactivated User
This seems like an simple problem, but I have to ask. I am simply trying to allow for clustering to be turned off and on for a query results graphic layer. The graphic layer has simple clustering enabled as default within the XAML.

I am providing a checkbox to allow for someone to turn the clustering off or on for the graphic layer.

I can figure out how to shut off the clustering using the simple code below, however I am not sure what would be the best way to allow for someone to turn the clustering back on.

For clusting off I am using a simple
Checkbox unchecked
GraphicsLayer layer = Map.Layers["QueryGraphicLayer"] as GraphicsLayer;
Layer.Clusterer = null

Does anyone have an example code of how they allow users to turn simple clustering back on for a layer. I was trying to use the example below, however this is for a specific custom clustering using an aggragete value field. I am simple trying to enable simple clustering.
0 Kudos
6 Replies
DarinaTchountcheva
Frequent Contributor
Hi Gabriel,

Here is one way to do this:

Cluster cluster; 

private void Map_Loaded(object sender, RoutedEventArgs e)
        {
            
            GraphicsLayer gl = Map.Layers["QueryGraphicsLayer"] as GraphicsLayer;

            //preserve the clusterer object to use it when you want to turn the clusering back on
            cluster = gl.Clusterer;

     //this is the symbol to be used for the single points that don't get clustered
            //it can be defined in your xaml, or you can difine it in code
     //if it is in your xaml you get it this way:
            SimpleRenderer simpleRenderer = LayoutRoot.Resouces["simpleRenderer"] as SimpleRenderer;

            //you can use a UniqueValueRenderer also, or not use anything
            //if you don't specify the renderer, the single points will be red circles (I think)

            gl.Renderer = simpleRenderer; 
            
        }


private void clusterCheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            
            GraphicsLayer gl = Map.Layers["QueryGraphicsLayer"] as GraphicsLayer;
            gl.Clusterer = null;

            UniqueValueRenderer uniqueRenderer = LayoutRoot.Resources[QueryUniqueValueRenderer"] as UniqueValueRenderer;
            gl.Renderer = uniqueRenderer;            
           
        }


private void clusterCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            
            GraphicsLayer gl = Map.Layers["QueryGraphicsLayer"] as GraphicsLayer;
            gl.Clusterer = clusterer;

            SimpleRenderer simpleRenderer = LayoutRoot.Resouces["simpleRenderer"] as SimpleRenderer;
            gl.Renderer = simpleRenderer;            
           
        }




Good Luck!

Darina

P.S. Check for spelling mistakes, I just typed this in notepad.
0 Kudos
gabrielvazquez
Deactivated User
Darina,
Thanks again for your help. I am still having issues when it comes to the CheckBox_Checked/Unchecked.

I am recieving an error "NullReferenceException was unhandled by user code" for the

GraphicsLayer gl = Map.Layers["QueryGraphicLayer"] as GraphicsLayer;

I've isolated the code you provided and for some reason the Map object continues to come up as null. Not sure why.
0 Kudos
DarinaTchountcheva
Frequent Contributor
Accidently hit Enter and posted too soon.
0 Kudos
DarinaTchountcheva
Frequent Contributor
Gabriel,

I know I am replying a little late (it's summer time), but you can either add the checkbox handlers in code behind in Map_Loaded instead in xaml, or you can check in the Check/Uncheck event handlers if the Map object is null, and skip the code if it is.

Cluster cluster; 

private void Map_Loaded(object sender, RoutedEventArgs e)
        {
            
            GraphicsLayer gl = Map.Layers["QueryGraphicsLayer"] as GraphicsLayer;

            //preserve the clusterer object to use it when you want to turn the clusering back on
            cluster = gl.Clusterer;

     //this is the symbol to be used for the single points that don't get clustered
            //it can be defined in your xaml, or you can difine it in code
     //if it is in your xaml you get it this way:
            SimpleRenderer simpleRenderer = LayoutRoot.Resouces["simpleRenderer"] as SimpleRenderer;

            //you can use a UniqueValueRenderer also, or not use anything
            //if you don't specify the renderer, the single points will be red circles (I think)

            gl.Renderer = simpleRenderer; 
            clusterCheckBox.Checked += new RoutedEventHandler(clusterCheckBox_Checked);
            clusterCheckBox.Unchecked += new RoutedEventHandler(clusterCheckBox_Unchecked);
            
        }


private void clusterCheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            //check first if Map is null
            if (Map == null) return;
            GraphicsLayer gl = Map.Layers["QueryGraphicsLayer"] as GraphicsLayer;
            //check if gl is null
            if (gl == null) return;

            gl.Clusterer = null;

            UniqueValueRenderer uniqueRenderer = LayoutRoot.Resources[QueryUniqueValueRenderer"] as UniqueValueRenderer;
            gl.Renderer = uniqueRenderer;   
           
        }


private void clusterCheckBox_Checked(object sender, RoutedEventArgs e)
        {
            //check first if Map is null
            if (Map == null) return;
            GraphicsLayer gl = Map.Layers["QueryGraphicsLayer"] as GraphicsLayer;
            //check if gl is null
            if (gl == null) return;

            GraphicsLayer gl = Map.Layers["QueryGraphicsLayer"] as GraphicsLayer;
            gl.Clusterer = clusterer;

            SimpleRenderer simpleRenderer = LayoutRoot.Resouces["simpleRenderer"] as SimpleRenderer;
            gl.Renderer = simpleRenderer;            
           
        }.


In your xaml remove the event handlers on the checkbox:
Checked="clusterCheckBox_Checked" and Unchecked="clusterCheckBox_Unchecked".

Hope this works!

Good Luck!
0 Kudos
gabrielvazquez
Deactivated User
Darina,
Np on the time of your response. I appreciate your help, I consider my self a very novice coder, so your help is much appreciated. I was able to get it work. I made some minor changes to the code and where I placed it, but I was able to get it to work.

Thanks again for your help.
0 Kudos
ChristopherHill
Deactivated User
We have a sample on our sdk site that does exactly what you are trying to do.
http://help.arcgis.com/en/webapi/silverlight/samples/start.htm#CustomClusterer
0 Kudos