Add graphics with symbols

View inMAUIUWPWPFWinUIView on GitHub

Use a symbol style to display a graphic on a graphics overlay.

Image of Add graphics with symbols

Use case

Allows you to customize a graphic by assigning a unique symbol. For example, you may wish to display individual graphics for different landmarks across a region, and to style each one with a unique symbol.

How to use the sample

Observe the graphics on the map.

How it works

  1. Create a GraphicsOverlay and add it to the MapView.
  2. Create a Symbol such as SimpleMarkerSymbol, SimpleLineSymbol or SimpleFillSymbol.
  3. Create a Graphic, specifying a Geometry and a Symbol.
  4. Add the Graphic to the GraphicsOverlay.

Relevant API

  • Geometry
  • Graphic
  • GraphicsOverlay
  • SimpleFillSymbol
  • SimpleLineSymbol
  • SimpleMarkerSymbol

Additional information

To set a symbol style across a number of graphics (e.g. showing trees as graphics sharing a symbol in a park), see the "Add graphics with renderer" sample.

Tags

display, fill, graphics, line, marker, overlay, point, symbol

Sample Code

AddGraphicsWithSymbols.xaml.csAddGraphicsWithSymbols.xaml.csAddGraphicsWithSymbols.xaml
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright 2022 Esri.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific
// language governing permissions and limitations under the License.

using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Symbology;
using Esri.ArcGISRuntime.UI;

using Colors = System.Drawing.Color;
using PointCollection = Esri.ArcGISRuntime.Geometry.PointCollection;

namespace ArcGIS.Samples.AddGraphicsWithSymbols
{
    [ArcGIS.Samples.Shared.Attributes.Sample(
        name: "Add graphics with symbols",
        category: "GraphicsOverlay",
        description: "Use a symbol style to display a graphic on a graphics overlay.",
        instructions: "Observe the graphics on the map.",
        tags: new[] { "display", "fill", "graphics", "line", "marker", "overlay", "point", "symbol" })]
    public partial class AddGraphicsWithSymbols : ContentPage
    {
        // Create the graphics overlay.
        private readonly GraphicsOverlay _overlay = new GraphicsOverlay();

        public AddGraphicsWithSymbols()
        {
            InitializeComponent();

            // Create the UI, setup the control references and execute initialization.
            Initialize();
        }

        private void Initialize()
        {
            // Create the map.
            Map myMap = new Map(BasemapStyle.ArcGISOceans);
            myMap.InitialViewpoint = new Viewpoint(56.075844, -2.681572, 13);

            // Add the map to the map view.
            MyMapView.Map = myMap;

            // Add the graphics overlay to the map view.
            MyMapView.GraphicsOverlays.Add(_overlay);

            // Call functions to create the graphics.
            CreatePoints();
            CreatePolygon();
            CreatePolyline();
            CreateText();

            // Update the extent to encompass all of the symbols.
            SetExtent();
        }

        private void CreatePoints()
        {
            // Create a red circle simple marker symbol.
            SimpleMarkerSymbol redCircleSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle.Circle, Colors.Red, 10);

            // Create graphics and add them to graphics overlay.
            Graphic graphic = new Graphic(new MapPoint(-2.72, 56.065, SpatialReferences.Wgs84), redCircleSymbol);
            _overlay.Graphics.Add(graphic);

            graphic = new Graphic(new MapPoint(-2.69, 56.065, SpatialReferences.Wgs84), redCircleSymbol);
            _overlay.Graphics.Add(graphic);

            graphic = new Graphic(new MapPoint(-2.66, 56.065, SpatialReferences.Wgs84), redCircleSymbol);
            _overlay.Graphics.Add(graphic);

            graphic = new Graphic(new MapPoint(-2.63, 56.065, SpatialReferences.Wgs84), redCircleSymbol);
            _overlay.Graphics.Add(graphic);
        }

        private void CreatePolyline()
        {
            // Create a purple simple line symbol.
            SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle.Dash, Colors.Purple, 4);

            // Create a new point collection for polyline.
            PointCollection points = new PointCollection(SpatialReferences.Wgs84)
            {
                // Create and add points to the point collection.
                new MapPoint(-2.715, 56.061),
                new MapPoint(-2.6438, 56.079),
                new MapPoint(-2.638, 56.079),
                new MapPoint(-2.636, 56.078),
                new MapPoint(-2.636, 56.077),
                new MapPoint(-2.637, 56.076),
                new MapPoint(-2.715, 56.061)
            };

            // Create the polyline from the point collection.
            Polyline polyline = new Polyline(points);

            // Create the graphic with polyline and symbol.
            Graphic graphic = new Graphic(polyline, lineSymbol);

            // Add graphic to the graphics overlay.
            _overlay.Graphics.Add(graphic);
        }

        private void CreatePolygon()
        {
            // Create a green simple line symbol.
            SimpleLineSymbol outlineSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle.Dash, Colors.Green, 1);

            // Create a green mesh simple fill symbol.
            SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbolStyle.DiagonalCross, Colors.Green, outlineSymbol);

            // Create a new point collection for polygon.
            PointCollection points = new PointCollection(SpatialReferences.Wgs84)
            {
                // Create and add points to the point collection.
                new MapPoint(-2.6425, 56.0784),
                new MapPoint(-2.6430, 56.0763),
                new MapPoint(-2.6410, 56.0759),
                new MapPoint(-2.6380, 56.0765),
                new MapPoint(-2.6380, 56.0784),
                new MapPoint(-2.6410, 56.0786)
            };

            // Create the polyline from the point collection.
            Polygon polygon = new Polygon(points);

            // Create the graphic with polyline and symbol.
            Graphic graphic = new Graphic(polygon, fillSymbol);

            // Add graphic to the graphics overlay.
            _overlay.Graphics.Add(graphic);
        }

        private void CreateText()
        {
            // Create two text symbols.
            TextSymbol bassRockTextSymbol = new TextSymbol("Black Rock", Colors.Blue, 10,
                Esri.ArcGISRuntime.Symbology.HorizontalAlignment.Left, Esri.ArcGISRuntime.Symbology.VerticalAlignment.Bottom);

            TextSymbol craigleithTextSymbol = new TextSymbol("Craigleith", Colors.Blue, 10,
                Esri.ArcGISRuntime.Symbology.HorizontalAlignment.Right, Esri.ArcGISRuntime.Symbology.VerticalAlignment.Top);

            // Create two points.
            MapPoint bassPoint = new MapPoint(-2.64, 56.079, SpatialReferences.Wgs84);
            MapPoint craigleithPoint = new MapPoint(-2.72, 56.076, SpatialReferences.Wgs84);

            // Create two graphics from the points and symbols.
            Graphic bassRockGraphic = new Graphic(bassPoint, bassRockTextSymbol);
            Graphic craigleithGraphic = new Graphic(craigleithPoint, craigleithTextSymbol);

            // Add graphics to the graphics overlay.
            _overlay.Graphics.Add(bassRockGraphic);
            _overlay.Graphics.Add(craigleithGraphic);
        }

        private void SetExtent()
        {
            // Get all of the graphics contained in the graphics overlay.
            GraphicCollection myGraphicCollection = _overlay.Graphics;

            // Create a new envelope builder using the same spatial reference as the graphics.
            EnvelopeBuilder myEnvelopeBuilder = new EnvelopeBuilder(SpatialReferences.Wgs84);

            // Loop through each graphic in the graphic collection.
            foreach (Graphic oneGraphic in myGraphicCollection)
            {
                // Union the extent of each graphic in the envelope builder.
                myEnvelopeBuilder.UnionOf(oneGraphic.Geometry.Extent);
            }

            // Expand the envelope builder by 30%.
            myEnvelopeBuilder.Expand(1.3);

            // Adjust the viewable area of the map to encompass all of the graphics in the
            // graphics overlay plus an extra 30% margin for better viewing.
            MyMapView.SetViewpointAsync(new Viewpoint(myEnvelopeBuilder.Extent));
        }
    }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.