[Expert lecture] ArcGIS API for Silverlight development (4): a bridge between users and geographic 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>

CopyCode

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:

 

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 achieveProgram. 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:

 

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 = 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 = 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.
Now, let's take a look at how to use the plot to interact with the map data.

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.