ArcGIS Server for Silverlight 之集群(Simple Clusterer)
前台代码:
Code Behind C#
效果图:
Code
<UserControl x:Class="SimpleClusterer.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:esri="clr-namespace:ESRI.ArcGIS.Client;assembly=ESRI.ArcGIS.Client"
xmlns:esriSymbols="clr-namespace:ESRI.ArcGIS.Client.Symbols;assembly=ESRI.ArcGIS.Client"
xmlns:esriGeometry ="clr-namespace:ESRI.ArcGIS.Client.Geometry;assembly=ESRI.ArcGIS.Client"
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
<Grid x:Name="LayoutRoot">
<esri:Map x:Name="myMap" ExtentChanged="myMap_ExtentChanged">
<esri:ArcGISTiledMapServiceLayer x:Name="myTiledMapServiceLayer"
Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
<esri:GraphicsLayer ID="mygraphicslayer">
</esri:GraphicsLayer>
</esri:Map>
</Grid>
</UserControl>
<UserControl x:Class="SimpleClusterer.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:esri="clr-namespace:ESRI.ArcGIS.Client;assembly=ESRI.ArcGIS.Client"
xmlns:esriSymbols="clr-namespace:ESRI.ArcGIS.Client.Symbols;assembly=ESRI.ArcGIS.Client"
xmlns:esriGeometry ="clr-namespace:ESRI.ArcGIS.Client.Geometry;assembly=ESRI.ArcGIS.Client"
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
<Grid x:Name="LayoutRoot">
<esri:Map x:Name="myMap" ExtentChanged="myMap_ExtentChanged">
<esri:ArcGISTiledMapServiceLayer x:Name="myTiledMapServiceLayer"
Url="http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer"/>
<esri:GraphicsLayer ID="mygraphicslayer">
</esri:GraphicsLayer>
</esri:Map>
</Grid>
</UserControl>
Code Behind C#
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Symbols;
using ESRI.ArcGIS.Client.Tasks;
namespace SimpleClusterer
{
public partial class MainPage : UserControl
{
GraphicsLayer graphicslayer = null;
SimpleMarkerSymbol simpleMarkerSymbol;
public MainPage()
{
InitializeComponent();
Init();
}
void Init()
{
//获取Graphicslayer
graphicslayer = myMap.Layers["mygraphicslayer"] as GraphicsLayer;
GradientStopCollection gradientStopColl = new GradientStopCollection();
GradientStop gstop1 = new GradientStop();
gstop1.Color = Colors.Red;
gstop1.Offset = 0;
GradientStop gstop2 = new GradientStop();
gstop2.Color = Colors.Gray;
gstop2.Offset = 0.25;
GradientStop gstop3 = new GradientStop();
gstop3.Color = Colors.Black;
gstop3.Offset = 0.5;
GradientStop gstop4 = new GradientStop();
gstop4.Color = Colors.Blue;
gstop4.Offset = 0.75;
GradientStop gstop5 = new GradientStop();
gstop5.Color = Colors.Green;
gstop5.Offset = 1;
gradientStopColl.Add(gstop1);
gradientStopColl.Add(gstop2);
gradientStopColl.Add(gstop3);
gradientStopColl.Add(gstop4);
gradientStopColl.Add(gstop5);
LinearGradientBrush mylinearGradientBrush = new LinearGradientBrush(gradientStopColl,0);
mylinearGradientBrush.MappingMode = BrushMappingMode.RelativeToBoundingBox;
//初始化simpleMarkerSymbol
SolidColorBrush symbolBrush = new SolidColorBrush(Colors.Purple);
simpleMarkerSymbol = new SimpleMarkerSymbol();
simpleMarkerSymbol.Size = 12;
simpleMarkerSymbol.Style = SimpleMarkerSymbol.SimpleMarkerStyle.Circle;
simpleMarkerSymbol.Color = symbolBrush;
//设置背景色
SolidColorBrush backbrush = new SolidColorBrush(Colors.Yellow);
//设置前景色
SolidColorBrush forebrush = new SolidColorBrush();
forebrush.Color = Color.FromArgb(99, 0, 0, 0);
//设置集群
FlareClusterer myflareClusterer = new FlareClusterer();
myflareClusterer.FlareBackground = backbrush;
myflareClusterer.FlareForeground = forebrush;
myflareClusterer.Radius = 10;
myflareClusterer.MaximumFlareCount = 20; //最大个数
myflareClusterer.Gradient = mylinearGradientBrush;
graphicslayer.Clusterer = myflareClusterer;
}
void LoadGraphic()
{
QueryTask querytask = new QueryTask();
querytask.Url = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/0";
querytask.ExecuteCompleted += new EventHandler<QueryEventArgs>(querytask_ExecuteCompleted);
Query query = new Query();
query.OutSpatialReferenceWKID = myMap.SpatialReference.WKID;
query.ReturnGeometry = true;
query.Where = "1=1";
querytask.ExecuteAsync(query);
}
void querytask_ExecuteCompleted(object sender, QueryEventArgs e)
{
FeatureSet featureset = e.FeatureSet;
if (featureset == null || featureset.Features.Count < 1)
{
MessageBox.Show("No features retured from query");
return;
}
foreach (Graphic g in featureset.Features)
{
g.Symbol = simpleMarkerSymbol;
graphicslayer.Graphics.Add(g);
}
}
private void myMap_ExtentChanged(object sender, ExtentEventArgs e)
{
if (e.OldExtent == null)
LoadGraphic();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Symbols;
using ESRI.ArcGIS.Client.Tasks;
namespace SimpleClusterer
{
public partial class MainPage : UserControl
{
GraphicsLayer graphicslayer = null;
SimpleMarkerSymbol simpleMarkerSymbol;
public MainPage()
{
InitializeComponent();
Init();
}
void Init()
{
//获取Graphicslayer
graphicslayer = myMap.Layers["mygraphicslayer"] as GraphicsLayer;
GradientStopCollection gradientStopColl = new GradientStopCollection();
GradientStop gstop1 = new GradientStop();
gstop1.Color = Colors.Red;
gstop1.Offset = 0;
GradientStop gstop2 = new GradientStop();
gstop2.Color = Colors.Gray;
gstop2.Offset = 0.25;
GradientStop gstop3 = new GradientStop();
gstop3.Color = Colors.Black;
gstop3.Offset = 0.5;
GradientStop gstop4 = new GradientStop();
gstop4.Color = Colors.Blue;
gstop4.Offset = 0.75;
GradientStop gstop5 = new GradientStop();
gstop5.Color = Colors.Green;
gstop5.Offset = 1;
gradientStopColl.Add(gstop1);
gradientStopColl.Add(gstop2);
gradientStopColl.Add(gstop3);
gradientStopColl.Add(gstop4);
gradientStopColl.Add(gstop5);
LinearGradientBrush mylinearGradientBrush = new LinearGradientBrush(gradientStopColl,0);
mylinearGradientBrush.MappingMode = BrushMappingMode.RelativeToBoundingBox;
//初始化simpleMarkerSymbol
SolidColorBrush symbolBrush = new SolidColorBrush(Colors.Purple);
simpleMarkerSymbol = new SimpleMarkerSymbol();
simpleMarkerSymbol.Size = 12;
simpleMarkerSymbol.Style = SimpleMarkerSymbol.SimpleMarkerStyle.Circle;
simpleMarkerSymbol.Color = symbolBrush;
//设置背景色
SolidColorBrush backbrush = new SolidColorBrush(Colors.Yellow);
//设置前景色
SolidColorBrush forebrush = new SolidColorBrush();
forebrush.Color = Color.FromArgb(99, 0, 0, 0);
//设置集群
FlareClusterer myflareClusterer = new FlareClusterer();
myflareClusterer.FlareBackground = backbrush;
myflareClusterer.FlareForeground = forebrush;
myflareClusterer.Radius = 10;
myflareClusterer.MaximumFlareCount = 20; //最大个数
myflareClusterer.Gradient = mylinearGradientBrush;
graphicslayer.Clusterer = myflareClusterer;
}
void LoadGraphic()
{
QueryTask querytask = new QueryTask();
querytask.Url = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer/0";
querytask.ExecuteCompleted += new EventHandler<QueryEventArgs>(querytask_ExecuteCompleted);
Query query = new Query();
query.OutSpatialReferenceWKID = myMap.SpatialReference.WKID;
query.ReturnGeometry = true;
query.Where = "1=1";
querytask.ExecuteAsync(query);
}
void querytask_ExecuteCompleted(object sender, QueryEventArgs e)
{
FeatureSet featureset = e.FeatureSet;
if (featureset == null || featureset.Features.Count < 1)
{
MessageBox.Show("No features retured from query");
return;
}
foreach (Graphic g in featureset.Features)
{
g.Symbol = simpleMarkerSymbol;
graphicslayer.Graphics.Add(g);
}
}
private void myMap_ExtentChanged(object sender, ExtentEventArgs e)
{
if (e.OldExtent == null)
LoadGraphic();
}
}
}
效果图: