其实ADF开发中也有GraphicsLayer的概念,同样在其他两个客户端API(JavaScript/Flex)中也能找到GraphicsLayer的身影,它们都是一样一样的。
本节我们主要看如何在GraphicsLayer中展现内容。当然第一个工作就是添加ESRI.ArcGIS.dll的引用,引入esri的xml命名空间;接下来在Map中添加一个GraphicsLayer图层:
- <esri:Map x:Name="Map1">
- <esri:Map.Layers>
- <!-- 其他图层 -->
- <esri:GraphicsLayer ID="GLayer" />
- </esri:Map.Layers>
- </esri:Map>
要让一个Graphic显示出来,总共分3步:
1、定义Graphic:
在xaml中
- <esri:Graphic>
- </esri:Graphic>
Graphic g= new Graphic()
2、设置Graphic的Geometry和Symbol属性:
在xaml中
- <esri:Graphic>
- <esri:Graphic.Symbol>
- <esriSymbols:SimpleMarkerSymbol Color="Blue" Size="12" Style="Square" />
- </esri:Graphic.Symbol>
- <esriGeometry:MapPoint X="108" Y="30" />
- </esri:Graphic>
- Graphic g = new Graphic()
- {
- Geometry = new MapPoint(108, 30),
- Symbol = new SimpleMarkerSymbol()
- {
- Color = new SolidColorBrush(Colors.Blue),
- Size = 12,
- Style = SimpleMarkerSymbol.SimpleMarkerStyle.Square
- }
- };
在xaml中
- <esri:GraphicsLayer ID="GLayer">
- <esri:GraphicsLayer.Graphics>
- <esri:Graphic>
- <esri:Graphic.Symbol>
- <esriSymbols:SimpleMarkerSymbol Color="Blue" Size="12" Style="Square" />
- </esri:Graphic.Symbol>
- <esriGeometry:MapPoint X="108" Y="30" />
- </esri:Graphic>
- </esri:GraphicsLayer.Graphics>
- </esri:GraphicsLayer>
- Graphic g = new Graphic()
- {
- Geometry = new MapPoint(108, 30),
- Symbol = new SimpleMarkerSymbol()
- {
- Color = new SolidColorBrush(Colors.Blue),
- Size = 12,
- Style = SimpleMarkerSymbol.SimpleMarkerStyle.Square
- }
- };
- GraphicsLayer glayer = Map1.Layers["GLayer"] as GraphicsLayer;
- glayer.Graphics.Add(g);
图中还有其他的图形,无非是改变了Graphic的Geometry和Symbol属性。图上的那只灰熊是一段动画文件,利用Silverlight的特性,能够定义出表现力丰富的各种符号。
尽管能够完全在xaml中来完成工作,但还是建议将可视化元素的定义放在xaml中,将实现的逻辑部分放在code-behind中。看一下添加图中那些Graphic的代码:
- <Grid.Resources>
- <esriSymbols:SimpleMarkerSymbol x:Name="RedMarkerSymbol" Color="Red" Size="12" Style="Circle" />
- <!-- 可惜目前Silverlight只支持Jpeg和PNG格式的图像,所以PictureMarkerSymbol无法显示GIF格式的图像,否则会报ImagingError的错误 -->
- <esriSymbolsictureMarkerSymbol x:Name="PinPictureMarkerSymbol" Source="imgs/pin.png" OffsetX="10" OffsetY="10" />
- <esriSymbols:SimpleLineSymbol x:Name="RedLineSymbol" Color="Red" Width="4" Style="Solid" />
- <esriSymbols:CartographicLineSymbol x:Name="CartoLineSymbol" Color="Red" Width="10" DashCap="Triangle" LineJoin="Round" DashArray="6,2" />
- <esriSymbols:SimpleFillSymbol x:Name="RedFillSymbol" Fill="#66FF0000" BorderBrush="Red" BorderThickness="2" />
- </Grid.Resources>
- <MediaElement x:Name="BearVideo" />
- private void AddGraphics()
- {
- GraphicsLayer glayer = Map1.Layers["GLayer"] as GraphicsLayer;
- Graphic[] graphics = new Graphic[8];
- graphics[0] = new Graphic()
- {
- Geometry = new MapPoint(108, 34),
- Symbol = RedMarkerSymbol
- };
- graphics[1] = new Graphic()
- {
- Geometry = new MapPoint(108, 30),
- Symbol = new SimpleMarkerSymbol()
- {
- Color = new SolidColorBrush(Colors.Blue),
- Size = 12,
- Style = SimpleMarkerSymbol.SimpleMarkerStyle.Square
- }
- };
- graphics[2] = new Graphic()
- {
- Geometry = new MapPoint(108, 25),
- Symbol = PinPictureMarkerSymbol
- };
- graphics[3] = new Graphic()
- {
- Geometry = new MapPoint(108, 20),
- Symbol = new TextSymbol()
- {
- FontFamily = new FontFamily("微软雅黑, 宋体"),
- FontSize = 14,
- Foreground = new SolidColorBrush(Colors.Black),
- Text = "这是text symbol"
- }
- };
- graphics[4] = new Graphic();
- graphics[4].Symbol = RedLineSymbol;
- ESRI.ArcGIS.Geometry.PointCollection pc = new ESRI.ArcGIS.Geometry.PointCollection()
- {
- new MapPoint(95,10),
- new MapPoint(110,-15),
- new MapPoint(130,10)
- };
- ESRI.ArcGIS.Geometry.Polyline pl = new ESRI.ArcGIS.Geometry.Polyline();
- pl.Paths.Add(pc);
- graphics[4].Geometry = pl;
- graphics[5] = new Graphic();
- graphics[5].Symbol = CartoLineSymbol;
- ESRI.ArcGIS.Geometry.PointCollection pc1 = new ESRI.ArcGIS.Geometry.PointCollection()
- {
- new MapPoint(95,0),
- new MapPoint(110,-25),
- new MapPoint(130,0)
- };
- ESRI.ArcGIS.Geometry.Polyline pl1 = new ESRI.ArcGIS.Geometry.Polyline();
- pl1.Paths.Add(pc1);
- graphics[5].Geometry = pl1;
- graphics[6] = new Graphic()
- {
- Symbol = RedFillSymbol
- };
- ESRI.ArcGIS.Geometry.PointCollection pc2 = new ESRI.ArcGIS.Geometry.PointCollection()
- {
- new MapPoint(110,-30),
- new MapPoint(130,-30),
- new MapPoint(130,-45),
- new MapPoint(120,-55),
- new MapPoint(110,-45),
- new MapPoint(110,-30)
- };
- ESRI.ArcGIS.Geometry.Polygon pg = new ESRI.ArcGIS.Geometry.Polygon();
- pg.Rings.Add(pc2);
- graphics[6].Geometry=pg;
- graphics[7] = new Graphic();
- //MediaElement的Name属性只能在xaml中定义(见帮助),所以决定了MediaElement不能完全在cs代码中定义
- BearVideo.Source = new Uri("http://serverapps.esri.com/media/bear.wmv", UriKind.RelativeOrAbsolute);
- BearVideo.IsHitTestVisible=false;
- BearVideo.IsMuted=true;
- BearVideo.AutoPlay=true;
- BearVideo.Opacity=0;
- ESRI.ArcGIS.Geometry.Polygon pg2 = new ESRI.ArcGIS.Geometry.Polygon();
- ESRI.ArcGIS.Geometry.PointCollection pc3 = new ESRI.ArcGIS.Geometry.PointCollection()
- {
- new MapPoint(10,-20),
- new MapPoint(32,7),
- new MapPoint(62,-35),
- new MapPoint(11,-36),
- new MapPoint(10,-20)
- };
- pg2.Rings.Add(pc3);
- graphics[7].Geometry=pg2;
- graphics[7].Symbol = new SimpleFillSymbol()
- {
- Fill = new VideoBrush()
- {
- SourceName = BearVideo.Name,
- Opacity = 0.6,
- Stretch = Stretch.UniformToFill
- }
- };
- foreach (Graphic g in graphics)
- {
- glayer.Graphics.Add(g);
- g.MouseLeftButtonDown+=new MouseButtonEventHandler(graphic_MouseLeftButtonDown);
- }
- }
- private void graphic_MouseLeftButtonDown(object o,MouseButtonEventArgs e)
- {
- Graphic g=o as Graphic;
- MessageBox.Show(string.Format("Geometry:{0}\nSymbol:{1}",g.Geometry.GetType().ToString(),g.Symbol.GetType().ToString()));
- }
可以把Draw理解成一块画板,调用Draw的Active()方法,就可以开始在画板上面绘画,程序会自动记录鼠标画出的每个Geometry,调用DeActive()方法,停止绘画。Active()有一个DrawMode参数,它决定了我们即将在这个画板上画出的内容类型:Point,Polyline,Polygon等。在画的过程中我们可以看到地图上可以实时反映出我们绘画的内容,而这些则利用了Draw的预定义Symbol:DefaultMarkerSymbol,DefaultLineSymbol,DefaultPolygonSymbol等。对应关系如下:
每当完成一个图形的绘制,就会触发Draw.OnDrawComplete事件,利用事件参数就可以获得Geometry,之后可以创建一个Graphic,设置一个Symbol(一般使用Draw的预定义Symbol),把画好的这个Graphic添加到一个GraphicsLayer中。
点击这里,查看一个比较完整的Graphics的例子。
最后来看一下这个例子的部分代码:
- <Grid.Resources>
- <esriSymbols:SimpleMarkerSymbol x:Name="DefaultMarkerSymbol" Color="Red" Size="12" Style="Circle" />
- <esriSymbols:CartographicLineSymbol x:Name="DefaultLineSymbol" Color="Red" Width="4" />
- <esriSymbols:SimpleFillSymbol x:Name="DefaultFillSymbol" Fill="#33FF0000" BorderBrush="Red" BorderThickness="2" />
- <esriSymbols:SimpleFillSymbol x:Name="DefaultPolygonSymbol" Fill="#33FF0000" BorderBrush="Red" BorderThickness="2" />
- </Grid.Resources>
- <esriraw x:Name="Draw1"
- DefaultRectangleSymbol="{StaticResource DefaultFillSymbol}"
- DefaultMarkerSymbol="{StaticResource DefaultMarkerSymbol}"
- DefaultLineSymbol="{StaticResource DefaultLineSymbol}"
- DefaultPolygonSymbol="{StaticResource DefaultPolygonSymbol}"
- Loaded="Draw1_Loaded"
- OnDrawComplete="Draw1_OnDrawComplete" />
- <Canvas VerticalAlignment="Top" HorizontalAlignment="Left" Margin="20,20,0,0" Width="430" Height="110">
- <Rectangle RadiusX="10" RadiusY="10" Width="430" Height="110" Fill="#98000000" Stroke="#FF6495ED" />
- <Rectangle Fill="#FFFFFFFF" Stroke="DarkGray" RadiusX="5" RadiusY="5" Canvas.Left="10" Canvas.Top="10" Width="410" Height="90" />
- <StackPanel Orientation="Vertical" Canvas.Top="5" Canvas.Left="20">
- <esriWidgets:Toolbar x:Name="ToolBar1" MaxItemHeight="80" MaxItemWidth="80" Width="380" Height="80"
- ToolbarIndexChanged="ToolBar1_ToolbarIndexChanged"
- ToolbarItemClicked="ToolBar1_ToolbarItemClicked">
- <esriWidgets:Toolbar.Items>
- <esriWidgets:ToolbarItemCollection>
- <esriWidgets:ToolbarItem Text="添加点">
- <esriWidgets:ToolbarItem.Content>
- <Image Source="imgs/DrawPoint.png" Stretch="UniformToFill" Margin="5" />
- </esriWidgets:ToolbarItem.Content>
- </esriWidgets:ToolbarItem>
- <esriWidgets:ToolbarItem Text="添加折线">
- <esriWidgets:ToolbarItem.Content>
- <Image Source="imgs/DrawPolyline.png" Stretch="UniformToFill" Margin="5" />
- </esriWidgets:ToolbarItem.Content>
- </esriWidgets:ToolbarItem>
- <esriWidgets:ToolbarItem Text="添加多边形">
- <esriWidgets:ToolbarItem.Content>
- <Image Source="imgs/DrawPolygon.png" Stretch="UniformToFill" Margin="5" />
- </esriWidgets:ToolbarItem.Content>
- </esriWidgets:ToolbarItem>
- <esriWidgets:ToolbarItem Text="添加矩形">
- <esriWidgets:ToolbarItem.Content>
- <Image Source="imgs/DrawRectangle.png" Stretch="UniformToFill" Margin="5" />
- </esriWidgets:ToolbarItem.Content>
- </esriWidgets:ToolbarItem>
- <esriWidgets:ToolbarItem Text="添加曲线">
- <esriWidgets:ToolbarItem.Content>
- <Image Source="imgs/DrawFreehand.png" Stretch="UniformToFill" Margin="5" />
- </esriWidgets:ToolbarItem.Content>
- </esriWidgets:ToolbarItem>
- <esriWidgets:ToolbarItem Text="停止添加动作">
- <esriWidgets:ToolbarItem.Content>
- <Image Source="imgs/StopDraw.png" Stretch="UniformToFill" Margin="5" />
- </esriWidgets:ToolbarItem.Content>
- </esriWidgets:ToolbarItem>
- <esriWidgets:ToolbarItem Text="清空绘制的图形">
- <esriWidgets:ToolbarItem.Content>
- <Image Source="imgs/eraser.png" Stretch="UniformToFill" Margin="5" />
- </esriWidgets:ToolbarItem.Content>
- </esriWidgets:ToolbarItem>
- </esriWidgets:ToolbarItemCollection>
- </esriWidgets:Toolbar.Items>
- </esriWidgets:Toolbar>
- <TextBlock x:Name="StatusTextBlock" Text="" FontWeight="Bold" HorizontalAlignment="Center"/>
- </StackPanel>
- </Canvas>
- private void Draw1_Loaded(object sender, RoutedEventArgs e)
- {
- Draw1.Map = Map1;
- }
- private void Draw1_OnDrawComplete(object sender, ESRI.ArcGIS.DrawEventArgs args)
- {
- ESRI.ArcGIS.GraphicsLayer graphicsLayer = Map1.Layers["GLayer2"] as ESRI.ArcGIS.GraphicsLayer;
- ESRI.ArcGIS.Graphic graphic = new ESRI.ArcGIS.Graphic()
- {
- Geometry = args.Geometry,
- Symbol = _activeSymbol,
- };
- graphicsLayer.Graphics.Add(graphic);
- }
- private void ToolBar1_ToolbarIndexChanged(object sender, ESRI.ArcGIS.Widgets.SelectedToolbarItemArgs e)
- {
- StatusTextBlock.Text = e.Item.Text;
- }
- private void ToolBar1_ToolbarItemClicked(object sender, ESRI.ArcGIS.Widgets.SelectedToolbarItemArgs e)
- {
- Draw1.Deactivate();
- switch (e.Index)
- {
- case 0: // Point
- Draw1.Activate(ESRI.ArcGIS.DrawMode.Point);
- _activeSymbol = strobeSymbol;
- break;
- case 1: // Polyline
- Draw1.Activate(ESRI.ArcGIS.DrawMode.Polyline);
- _activeSymbol = DefaultLineSymbol;
- break;
- case 2: // Polygon
- Draw1.Activate(ESRI.ArcGIS.DrawMode.Polygon);
- _activeSymbol = DefaultPolygonSymbol;
- break;
- case 3: // Rectangle
- Draw1.Activate(ESRI.ArcGIS.DrawMode.Rectangle);
- _activeSymbol = DefaultFillSymbol;
- break;
- case 4: // Freehand
- Draw1.Activate(ESRI.ArcGIS.DrawMode.Freehand);
- _activeSymbol = waveLineSymbol;
- break;
- case 5: // Stop Graphics
- break;
- case 6: // Clear Graphics
- ESRI.ArcGIS.GraphicsLayer graphicsLayer = Map1.Layers["GLayer2"] as ESRI.ArcGIS.GraphicsLayer;
- graphicsLayer.ClearGraphics();
- break;
- }
- }
大家可以注意一下例子中添加的点符号和曲线符号。只要有足够的想象力,完全可以利用Silverlight定制出非常炫的符号效果来。
原文地址:http://bbs.esrichina-bj.cn/ESRI/thread-44892-1-1.html
本文转自温景良(Jason)博客园博客,原文链接:http://www.cnblogs.com/wenjl520/archive/2009/06/02/1494143.html,如需转载请自行联系原作者