When we interact with a map, we perform the following operations: a drop-down box or a query of the map content. Input and Output in these interactions are usually reflected on a layer independent of map data. For example, if the frame is enlarged, we can see a rectangle drawn by the mouse, or query the point of interest, the result is usually to highlight the shape of the interest point that meets the condition in the independent "layer", through which the user's input can be reflected, and the map output can be displayed. This "layer" is GraphicsLayer.
In fact, the concept of GraphicsLayer also exists in the development of the ADF. GraphicsLayer can also be found in the APIs of the other two clients (JavaScript/Flex). They are all the same.
This section describes how to display the content in GraphicsLayer. Of course, the first job is to add ESRI. ArcGIS. dll reference and introduce the xml namespace of esri. Next, add a GraphicsLayer layer Layer in Map:
- <Esri: Map x: Name = "Map1">
- <Esri: Map. Layers>
- <! -- Other layers -->
- <Esri: GraphicsLayer ID = "GLayer"/>
- </Esri: Map. Layers>
- </Esri: Map>
Copy code
To keep the content in GraphicsLayer at the top (not covered by other layers), place it at the bottom of the Map label, as shown above. From the naming, we can easily see that the GraphicLayer contains a set of Graphic. Graphic (ESRI. arcGIS. graphic) is the basic element in GraphicsLayer, which includes Geometry (in ESRI. arcGIS. geometry namespace), Symbol (in ESRI. arcGIS. in the Symbol namespace), Attributes, and other Attributes. All vector elements displayed on a map have a Geometry, which contains several geographical coordinates used to display the shape of objects on the map. It is a general term for Point, Polyline, and Polygon, it represents the shape of Graphic. Symbol represents the appearance of Graphic. It is a general term for a series of symbols. We usually deal with SimpleMarkerSymbol, SimpleLineSymbol, and SimpleFillSymbol. They correspond to the three different Geometry (Point, Polyline, polygon ).
To display a Graphic in three steps:
1. Define Graphic:
In xaml
- <Esri: Graphic>
- </Esri: Graphic>
Copy code
In code-behind
Graphic g = new Graphic ()
2. Set the Geometry and Symbol attributes of Graphic:
In 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>
Copy code
In code-behind
- Graphic g = new Graphic ()
- {
- Geometry = new MapPoint (108, 30 ),
- Symbol = new SimpleMarkerSymbol ()
- {
- Color = new SolidColorBrush (Colors. Blue ),
- Size = 12,
- Style = SimpleMarkerSymbol. SimpleMarkerStyle. Square
- }
- };
Copy code
3. Add the defined Graphic to GraphicsLayer:
In 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>
Copy code
In code-behind
- 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 );
Copy code
Let's take a look at the effect:
Download(246.21 KB)
There are other images in the figure, except that the Geometry and Symbol attributes of Graphic are changed. The grizzly bear in the figure is an animated file. Using Silverlight, it can define various expressive symbols.
Although the work can be completed completely in xaml, we recommend that you put the definition of the visualization element in xaml and put the logic part of the implementation in code-behind. Let's take a look at the Graphic code in the graph:
- <Grid. Resources>
- <EsriSymbols: SimpleMarkerSymbol x: Name = "RedMarkerSymbol" Color = "Red" Size = "12" Style = "Circle"/>
- <! -- Unfortunately, currently, Silverlight only supports Jpeg and PNG images. Therefore, PictureMarkerSymbol cannot display GIF images; otherwise, an ImagingError error occurs. -->
- <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"/>
Copy code
- 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 = "this is 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 ();
- // The Name attribute of MediaElement can only be defined in xaml (see help). Therefore, MediaElement cannot be completely defined in cs code.
- 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 ()));
- }
Copy code
We can see that some events can be defined on a Graphic to achieve the purpose of the program. You can try to rewrite the above content in xaml. The question is: is every definition of Geometry so difficult? In fact, SilverlightAPI has provided us with ESRI. arcGIS. draw (inherited from the Canvas in xaml) class, which can easily capture users' mouse operations and obtain various Geometry for use by the program.
You can understand Draw as a canvas and call the Active () method of Draw to Draw a drawing on the canvas. The program automatically records every Geometry drawn by the mouse and calls the DeActive () method, stop painting. Active () has a DrawMode parameter, which determines the content types we will draw on this canvas: Point, Polyline, Polygon, etc. During the painting process, we can see that the map can reflect the painting content in real time, and these use the predefined Symbol of Draw: DefaultMarkerSymbol, defalinlinesymbol, defapolypolygonsymbol, and so on. The relationship is as follows:
Download(5.8 KB)
Each time a drawing is drawn, Draw is triggered. onDrawComplete event, you can obtain Geometry using the event parameters, and then you can create a Graphic and set a Symbol (generally use the pre-defined Symbol of Draw ), add the Graphic to a GraphicsLayer.
Click here to view a complete Graphics example.
Finally, let's take a look at some of the Code in this example:
- <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 = "drawuploloaded"
- OnDrawComplete = "Draw1_OnDrawComplete"/>
- <Canvas verticalignment = "Top" HorizontalAlignment = "Left" Margin = "20, 20, 430" Width = "110" Height = "">
- <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 = "add point">
- <EsriWidgets: ToolbarItem. Content>
- <Image Source = "imgs/DrawPoint.png" Stretch = "UniformToFill" Margin = "5"/>
- </EsriWidgets: ToolbarItem. Content>
- </EsriWidgets: ToolbarItem>
- <EsriWidgets: ToolbarItem Text = "add a line">
- <EsriWidgets: ToolbarItem. Content>
- <Image Source = "imgs/DrawPolyline.png" Stretch = "UniformToFill" Margin = "5"/>
- </EsriWidgets: ToolbarItem. Content>
- </EsriWidgets: ToolbarItem>
- <EsriWidgets: ToolbarItem Text = "add polygon">
- <EsriWidgets: ToolbarItem. Content>
- <Image Source = "imgs/DrawPolygon.png" Stretch = "UniformToFill" Margin = "5"/>
- </EsriWidgets: ToolbarItem. Content>
- </EsriWidgets: ToolbarItem>
- <EsriWidgets: ToolbarItem Text = "add rectangle">
- <EsriWidgets: ToolbarItem. Content>
- <Image Source = "imgs/DrawRectangle.png" Stretch = "UniformToFill" Margin = "5"/>
- </EsriWidgets: ToolbarItem. Content>
- </EsriWidgets: ToolbarItem>
- <EsriWidgets: ToolbarItem Text = "add curve">
- <EsriWidgets: ToolbarItem. Content>
- <Image Source = "imgs/DrawFreehand.png" Stretch = "UniformToFill" Margin = "5"/>
- </EsriWidgets: ToolbarItem. Content>
- </EsriWidgets: ToolbarItem>
- <EsriWidgets: ToolbarItem Text = "Stop adding">
- <EsriWidgets: ToolbarItem. Content>
- <Image Source = "imgs/StopDraw.png" Stretch = "UniformToFill" Margin = "5"/>
- </EsriWidgets: ToolbarItem. Content>
- </EsriWidgets: ToolbarItem>
- <EsriWidgets: ToolbarItem Text = "Clear drawn image">
- <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>
Copy code
- Private void drawuploloaded (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;
- }
- }
Copy code
Note the dot and curve symbols added in the example. As long as you have enough imagination, you can use Silverlight to customize Very dazzling symbolic effects.
Address: http://bbs.esrichina-bj.cn/ESRI/thread-44892-1-1.html