ArcGIS API for Silverlight (4): a bridge between users and geographical information-GraphicsLayer

Source: Internet
Author: User
Tags polyline
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:

  1. <Esri: Map x: Name = "Map1">
  2. <Esri: Map. Layers>
  3. <! -- Other layers -->
  4. <Esri: GraphicsLayer ID = "GLayer"/>
  5. </Esri: Map. Layers>
  6. </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

  1. <Esri: Graphic>
  2. </Esri: Graphic>

Copy code

In code-behind
Graphic g = new Graphic ()
2. Set the Geometry and Symbol attributes of Graphic:
In xaml

  1. <Esri: Graphic>
  2. <Esri: Graphic. Symbol>
  3. <EsriSymbols: SimpleMarkerSymbol Color = "Blue" Size = "12" Style = "Square"/>
  4. </Esri: Graphic. Symbol>
  5. <EsriGeometry: MapPoint X = "108" Y = "30"/>
  6. </Esri: Graphic>

Copy code

In code-behind

  1. Graphic g = new Graphic ()
  2. {
  3. Geometry = new MapPoint (108, 30 ),
  4. Symbol = new SimpleMarkerSymbol ()
  5. {
  6. Color = new SolidColorBrush (Colors. Blue ),
  7. Size = 12,
  8. Style = SimpleMarkerSymbol. SimpleMarkerStyle. Square
  9. }
  10. };

Copy code

3. Add the defined Graphic to GraphicsLayer:
In xaml

  1. <Esri: GraphicsLayer ID = "GLayer">
  2. <Esri: GraphicsLayer. Graphics>
  3. <Esri: Graphic>
  4. <Esri: Graphic. Symbol>
  5. <EsriSymbols: SimpleMarkerSymbol Color = "Blue" Size = "12" Style = "Square"/>
  6. </Esri: Graphic. Symbol>
  7. <EsriGeometry: MapPoint X = "108" Y = "30"/>
  8. </Esri: Graphic>
  9. </Esri: GraphicsLayer. Graphics>
  10. </Esri: GraphicsLayer>

Copy code

In code-behind

  1. Graphic g = new Graphic ()
  2. {
  3. Geometry = new MapPoint (108, 30 ),
  4. Symbol = new SimpleMarkerSymbol ()
  5. {
  6. Color = new SolidColorBrush (Colors. Blue ),
  7. Size = 12,
  8. Style = SimpleMarkerSymbol. SimpleMarkerStyle. Square
  9. }
  10. };
  11. GraphicsLayer glayer = Map1.Layers ["GLayer"] as GraphicsLayer;
  12. 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:

  1. <Grid. Resources>
  2. <EsriSymbols: SimpleMarkerSymbol x: Name = "RedMarkerSymbol" Color = "Red" Size = "12" Style = "Circle"/>
  3. <! -- Unfortunately, currently, Silverlight only supports Jpeg and PNG images. Therefore, PictureMarkerSymbol cannot display GIF images; otherwise, an ImagingError error occurs. -->
  4. <EsriSymbolsictureMarkerSymbol x: Name = "PinPictureMarkerSymbol" Source = "imgs/pin.png" OffsetX = "10" OffsetY = "10"/>
  5. <EsriSymbols: SimpleLineSymbol x: Name = "RedLineSymbol" Color = "Red" Width = "4" Style = "Solid"/>
  6. <EsriSymbols: CartographicLineSymbol x: Name = "CartoLineSymbol" Color = "Red" Width = "10" DashCap = "Triangle" LineJoin = "Round" DashArray = "6, 2"/>
  7. <EsriSymbols: SimpleFillSymbol x: Name = "RedFillSymbol" Fill = "#66FF0000" BorderBrush = "Red" BorderThickness = "2"/>
  8. </Grid. Resources>
  9. <MediaElement x: Name = "BearVideo"/>

Copy code

  1. Private void AddGraphics ()
  2. {
  3. GraphicsLayer glayer = Map1.Layers ["GLayer"] as GraphicsLayer;
  4. Graphic [] graphics = new Graphic [8];
  5. Graphics [0] = new Graphic ()
  6. {
  7. Geometry = new MapPoint (108, 34 ),
  8. Symbol = RedMarkerSymbol
  9. };
  10. Graphics [1] = new Graphic ()
  11. {
  12. Geometry = new MapPoint (108, 30 ),
  13. Symbol = new SimpleMarkerSymbol ()
  14. {
  15. Color = new SolidColorBrush (Colors. Blue ),
  16. Size = 12,
  17. Style = SimpleMarkerSymbol. SimpleMarkerStyle. Square
  18. }
  19. };
  20. Graphics [2] = new Graphic ()
  21. {
  22. Geometry = new MapPoint (108, 25 ),
  23. Symbol = PinPictureMarkerSymbol
  24. };
  25. Graphics [3] = new Graphic ()
  26. {
  27. Geometry = new MapPoint (108, 20 ),
  28. Symbol = new TextSymbol ()
  29. {
  30. FontFamily = new FontFamily (", "),
  31. FontSize = 14,
  32. Foreground = new SolidColorBrush (Colors. Black ),
  33. Text = "this is text symbol"
  34. }
  35. };
  36. Graphics [4] = new Graphic ();
  37. Graphics [4]. Symbol = RedLineSymbol;
  38. ESRI. ArcGIS. Geometry. PointCollection pc = new ESRI. ArcGIS. Geometry. PointCollection ()
  39. {
  40. New MapPoint (95,10 ),
  41. New MapPoint (110,-15 ),
  42. New MapPoint (130,10)
  43. };
  44. ESRI. ArcGIS. Geometry. Polyline pl = new ESRI. ArcGIS. Geometry. Polyline ();
  45. Pl. Paths. Add (pc );
  46. Graphics [4]. Geometry = pl;
  47. Graphics [5] = new Graphic ();
  48. Graphics [5]. Symbol = CartoLineSymbol;
  49. ESRI. ArcGIS. Geometry. PointCollection pc1 = new ESRI. ArcGIS. Geometry. PointCollection ()
  50. {
  51. New MapPoint (95,0 ),
  52. New MapPoint (110,-25 ),
  53. New MapPoint (130,0)
  54. };
  55. ESRI. ArcGIS. Geometry. Polyline pl1 = new ESRI. ArcGIS. Geometry. Polyline ();
  56. Pl1.Paths. Add (pc1 );
  57. Graphics [5]. Geometry = pl1;
  58. Graphics [6] = new Graphic ()
  59. {
  60. Symbol = RedFillSymbol
  61. };
  62. ESRI. ArcGIS. Geometry. PointCollection pc2 = new ESRI. ArcGIS. Geometry. PointCollection ()
  63. {
  64. New MapPoint (110,-30 ),
  65. New MapPoint (130,-30 ),
  66. New MapPoint (130,-45 ),
  67. New MapPoint (120,-55 ),
  68. New MapPoint (110,-45 ),
  69. New MapPoint (110,-30)
  70. };
  71. ESRI. ArcGIS. Geometry. Polygon pg = new ESRI. ArcGIS. Geometry. Polygon ();
  72. Pg. Rings. Add (pc2 );
  73. Graphics [6]. Geometry = pg;
  74. Graphics [7] = new Graphic ();
  75. // The Name attribute of MediaElement can only be defined in xaml (see help). Therefore, MediaElement cannot be completely defined in cs code.
  76. BearVideo. Source = new Uri ("http://serverapps.esri.com/media/bear.wmv", UriKind. RelativeOrAbsolute );
  77. BearVideo. IsHitTestVisible = false;
  78. BearVideo. IsMuted = true;
  79. BearVideo. AutoPlay = true;
  80. BearVideo. Opacity = 0;
  81. ESRI. ArcGIS. Geometry. Polygon pg2 = new ESRI. ArcGIS. Geometry. Polygon ();
  82. ESRI. ArcGIS. Geometry. PointCollection pc3 = new ESRI. ArcGIS. Geometry. PointCollection ()
  83. {
  84. New MapPoint (10,-20 ),
  85. New MapPoint (32, 7 ),
  86. New MapPoint (62,-35 ),
  87. New MapPoint (11,-36 ),
  88. New MapPoint (10,-20)
  89. };
  90. Pg2.Rings. Add (pc3 );
  91. Graphics [7]. Geometry = pg2;
  92. Graphics [7]. Symbol = new SimpleFillSymbol ()
  93. {
  94. Fill = new VideoBrush ()
  95. {
  96. SourceName = BearVideo. Name,
  97. Opacity = 0.6,
  98. Stretch = Stretch. UniformToFill
  99. }
  100. };
  101. Foreach (Graphic g in graphics)
  102. {
  103. Glayer. Graphics. Add (g );
  104. G. MouseLeftButtonDown + = new MouseButtonEventHandler (graphic_MouseLeftButtonDown );
  105. }
  106. }
  107. Private void graphic_MouseLeftButtonDown (object o, MouseButtonEventArgs e)
  108. {
  109. Graphic g = o as Graphic;
  110. MessageBox. show (string. format ("Geometry: {0} \ nSymbol: {1}", g. geometry. getType (). toString (), g. symbol. getType (). toString ()));
  111. }

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:

  1. <Grid. Resources>
  2. <EsriSymbols: SimpleMarkerSymbol x: Name = "DefaultMarkerSymbol" Color = "Red" Size = "12" Style = "Circle"/>
  3. <EsriSymbols: CartographicLineSymbol x: Name = "DefaultLineSymbol" Color = "Red" Width = "4"/>
  4. <EsriSymbols: SimpleFillSymbol x: Name = "DefaultFillSymbol" Fill = "#33FF0000" BorderBrush = "Red" BorderThickness = "2"/>
  5. <EsriSymbols: SimpleFillSymbol x: Name = "DefaultPolygonSymbol" Fill = "#33FF0000" BorderBrush = "Red" BorderThickness = "2"/>
  6. </Grid. Resources>
  7. <Esriraw x: Name = "Draw1"
  8. DefaultRectangleSymbol = "{StaticResource DefaultFillSymbol }"
  9. DefaultMarkerSymbol = "{StaticResource DefaultMarkerSymbol }"
  10. DefaultLineSymbol = "{StaticResource DefaultLineSymbol }"
  11. DefaultPolygonSymbol = "{StaticResource DefaultPolygonSymbol }"
  12. Loaded = "drawuploloaded"
  13. OnDrawComplete = "Draw1_OnDrawComplete"/>
  14. <Canvas verticalignment = "Top" HorizontalAlignment = "Left" Margin = "20, 20, 430" Width = "110" Height = "">
  15. <Rectangle RadiusX = "10" RadiusY = "10" Width = "430" Height = "110" Fill = "#98000000" Stroke = "# FF6495ED"/>
  16. <Rectangle Fill = "# FFFFFFFF" Stroke = "DarkGray" RadiusX = "5" RadiusY = "5" Canvas. left = "10" Canvas. top = "10" Width = "410" Height = "90"/>
  17. <StackPanel Orientation = "Vertical" Canvas. Top = "5" Canvas. Left = "20">
  18. <EsriWidgets: Toolbar x: Name = "ToolBar1" MaxItemHeight = "80" MaxItemWidth = "80" Width = "380" Height = "80"
  19. ToolbarIndexChanged = "ToolBar1_ToolbarIndexChanged"
  20. ToolbarItemClicked = "ToolBar1_ToolbarItemClicked">
  21. <EsriWidgets: Toolbar. Items>
  22. <EsriWidgets: ToolbarItemCollection>
  23. <EsriWidgets: ToolbarItem Text = "add point">
  24. <EsriWidgets: ToolbarItem. Content>
  25. <Image Source = "imgs/DrawPoint.png" Stretch = "UniformToFill" Margin = "5"/>
  26. </EsriWidgets: ToolbarItem. Content>
  27. </EsriWidgets: ToolbarItem>
  28. <EsriWidgets: ToolbarItem Text = "add a line">
  29. <EsriWidgets: ToolbarItem. Content>
  30. <Image Source = "imgs/DrawPolyline.png" Stretch = "UniformToFill" Margin = "5"/>
  31. </EsriWidgets: ToolbarItem. Content>
  32. </EsriWidgets: ToolbarItem>
  33. <EsriWidgets: ToolbarItem Text = "add polygon">
  34. <EsriWidgets: ToolbarItem. Content>
  35. <Image Source = "imgs/DrawPolygon.png" Stretch = "UniformToFill" Margin = "5"/>
  36. </EsriWidgets: ToolbarItem. Content>
  37. </EsriWidgets: ToolbarItem>
  38. <EsriWidgets: ToolbarItem Text = "add rectangle">
  39. <EsriWidgets: ToolbarItem. Content>
  40. <Image Source = "imgs/DrawRectangle.png" Stretch = "UniformToFill" Margin = "5"/>
  41. </EsriWidgets: ToolbarItem. Content>
  42. </EsriWidgets: ToolbarItem>
  43. <EsriWidgets: ToolbarItem Text = "add curve">
  44. <EsriWidgets: ToolbarItem. Content>
  45. <Image Source = "imgs/DrawFreehand.png" Stretch = "UniformToFill" Margin = "5"/>
  46. </EsriWidgets: ToolbarItem. Content>
  47. </EsriWidgets: ToolbarItem>
  48. <EsriWidgets: ToolbarItem Text = "Stop adding">
  49. <EsriWidgets: ToolbarItem. Content>
  50. <Image Source = "imgs/StopDraw.png" Stretch = "UniformToFill" Margin = "5"/>
  51. </EsriWidgets: ToolbarItem. Content>
  52. </EsriWidgets: ToolbarItem>
  53. <EsriWidgets: ToolbarItem Text = "Clear drawn image">
  54. <EsriWidgets: ToolbarItem. Content>
  55. <Image Source = "imgs/eraser.png" Stretch = "UniformToFill" Margin = "5"/>
  56. </EsriWidgets: ToolbarItem. Content>
  57. </EsriWidgets: ToolbarItem>
  58. </EsriWidgets: ToolbarItemCollection>
  59. </EsriWidgets: Toolbar. Items>
  60. </EsriWidgets: Toolbar>
  61. <TextBlock x: Name = "StatusTextBlock" Text = "" FontWeight = "Bold" HorizontalAlignment = "Center"/>
  62. </StackPanel>
  63. </Canvas>

Copy code

  1. Private void drawuploloaded (object sender, RoutedEventArgs e)
  2. {
  3. Draw1.Map = Map1;
  4. }
  5. Private void Draw1_OnDrawComplete (object sender, ESRI. ArcGIS. DrawEventArgs args)
  6. {
  7. ESRI. ArcGIS. GraphicsLayer graphicsLayer = Map1.Layers ["GLayer2"] as ESRI. ArcGIS. GraphicsLayer;
  8. ESRI. ArcGIS. Graphic graphic = new ESRI. ArcGIS. Graphic ()
  9. {
  10. Geometry = args. Geometry,
  11. Symbol = _ activeSymbol,
  12. };
  13. GraphicsLayer. Graphics. Add (graphic );
  14. }
  15. Private void ToolBar1_ToolbarIndexChanged (object sender, ESRI. ArcGIS. Widgets. SelectedToolbarItemArgs e)
  16. {
  17. StatusTextBlock. Text = e. Item. Text;
  18. }
  19. Private void ToolBar1_ToolbarItemClicked (object sender, ESRI. ArcGIS. Widgets. SelectedToolbarItemArgs e)
  20. {
  21. Draw1.Deactivate ();
  22. Switch (e. Index)
  23. {
  24. Case 0: // Point
  25. Draw1.Activate (ESRI. ArcGIS. DrawMode. Point );
  26. _ ActiveSymbol = strobeSymbol;
  27. Break;
  28. Case 1: // Polyline
  29. Draw1.Activate (ESRI. ArcGIS. DrawMode. Polyline );
  30. _ ActiveSymbol = DefaultLineSymbol;
  31. Break;
  32. Case 2: // Polygon
  33. Draw1.Activate (ESRI. ArcGIS. DrawMode. Polygon );
  34. _ ActiveSymbol = DefaultPolygonSymbol;
  35. Break;
  36. Case 3: // Rectangle
  37. Draw1.Activate (ESRI. ArcGIS. DrawMode. Rectangle );
  38. _ ActiveSymbol = DefaultFillSymbol;
  39. Break;
  40. Case 4: // Freehand
  41. Draw1.Activate (ESRI. ArcGIS. DrawMode. Freehand );
  42. _ ActiveSymbol = waveLineSymbol;
  43. Break;
  44. Case 5: // Stop Graphics
  45. Break;
  46. Case 6: // Clear Graphics
  47. ESRI. ArcGIS. GraphicsLayer graphicsLayer = Map1.Layers ["GLayer2"] as ESRI. ArcGIS. GraphicsLayer;
  48. GraphicsLayer. ClearGraphics ();
  49. Break;
  50. }
  51. }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.