Style geometry types with symbols

View inMAUIWPFWinUIView on GitHubSample viewer app

Use a symbol to display a geometry on a map.

Screenshot of Style geometry types with symbols sample

Use case

Customize the appearance of a geometry type with a symbol style suitable for the data. For example, a tourism office may use pictures of landmarks as symbols on an online map or app to help prospective visitors orient themselves more easily around a city. A point on the map styled with a circle could represent a drilled borehole location, whereas a cross could represent the location of an old coal mine shaft. A red line with a dashed style could represent a geological fault mapped on a geological map. A polygon with a brown 'forward-diagonal' fill style could represent an area of artificial ground mapped on a geological map.

How to use the sample

Tap "Edit Styles" and select a geometry to edit with the picker. Use the controls to change the symbol properties for the geometry.

How it works

  1. Create a PictureMarkerSymbol or SimpleMarkerSymbol to style a Point.
    • For the picture marker symbol, create it using a URL or image and set its height property.
    • For the simple marker symbol, set the Style, Color, and Size properties.
  2. Create a SimpleLineSymbol to style a Polyline.
    • Set the Style, Color, and Size properties.
  3. Create a SimpleFillSymbol to style a Polygon.
    • Set the Style, Color, and Outline properties.
  4. Create Graphics using the geometries and symbols and add them to a GraphicsOverlay.
  5. Add the graphics overlay to a MapView.

Relevant API

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

Tags

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

Sample Code

StyleGeometryTypesWithSymbols.xaml.csStyleGeometryTypesWithSymbols.xaml.csStyleGeometryTypesWithSymbols.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// Copyright 2024 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 System.Windows.Controls;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using Color = System.Drawing.Color;
using System.Reflection;
using TabControl = System.Windows.Controls.TabControl;
using System.Windows.Media;
using Button = System.Windows.Controls.Button;

namespace ArcGIS.WPF.Samples.StyleGeometryTypesWithSymbols
{
    [ArcGIS.Samples.Shared.Attributes.Sample(
        name: "Style geometry types with symbols",
        category: "Symbology",
        description: "Use a symbol to display a geometry on a map.",
        instructions: "Tap \"Edit Styles\" and select a geometry to edit with the picker. Use the controls to change the symbol properties for the geometry.",
        tags: new[] { "display", "fill", "graphics", "line", "marker", "overlay", "picture", "point", "symbol", "visualization" })]
    [ArcGIS.Samples.Shared.Attributes.OfflineData()]
    public partial class StyleGeometryTypesWithSymbols
    {
        // Item sources for the combo boxes.
        public List<SimpleMarkerSymbolStyle> SimpleMarkerSymbolStyles => Enum.GetValues(typeof(SimpleMarkerSymbolStyle)).Cast<SimpleMarkerSymbolStyle>().ToList();
        public List<SimpleLineSymbolStyle> SimpleLineSymbolStyles => Enum.GetValues(typeof(SimpleLineSymbolStyle)).Cast<SimpleLineSymbolStyle>().ToList();
        public List<SimpleFillSymbolStyle> SimpleFillSymbolStyles => Enum.GetValues(typeof(SimpleFillSymbolStyle)).Cast<SimpleFillSymbolStyle>().ToList();

        // Hold the selected graphic.
        private Graphic _selectedGraphic;

        // Flag indicating if styling should be applied to a polygon's fill or outline.
        private bool _stylingPolygonFill = true;

        public StyleGeometryTypesWithSymbols()
        {
            InitializeComponent();
            _ = Initialize();
        }

        private async Task Initialize()
        {
            // Set the data context for UI bindings.
            DataContext = this;

            // Create a new map with a topographic basemap initially centered on Woolgarston, England.
            MyMapView.Map = new Map(BasemapStyle.ArcGISTopographic)
            {
                InitialViewpoint = new Viewpoint(new MapPoint(-225e3, 6_553e3, SpatialReferences.WebMercator), 88e3)
            };

            // A graphics overlay for displaying the geometry graphics on the map view.
            var graphicsOverlay = new GraphicsOverlay();

            // Create the simple marker symbol for styling the point.
            var pointSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle.Circle, Color.Purple, 12);

            // Create simple line symbol for styling the polyline.
            var polylineSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle.Dash, Color.Red, 6);

            // Create the simple fill symbol for styling the polygon, including its outline.
            var polygonSymbol = new SimpleFillSymbol(SimpleFillSymbolStyle.ForwardDiagonal, Color.Blue,
                new SimpleLineSymbol(SimpleLineSymbolStyle.Solid, Color.Green, 3));

            // Create a point graphic and add it to the graphics overlay.
            var point = new MapPoint(-225e3, 6_560e3, SpatialReferences.WebMercator);
            var pointGraphic = new Graphic(point, pointSymbol);
            graphicsOverlay.Graphics.Add(pointGraphic);

            // Create a polyline graphic and add it to the graphics overlay.
            var points = new MapPoint[2]
            {
                new MapPoint(-223e3, 6_559e3, SpatialReferences.WebMercator),
                new MapPoint(-227e3, 6_559e3, SpatialReferences.WebMercator)
            };
            var polyline = new Polyline(points, SpatialReferences.WebMercator);
            var polylineGraphic = new Graphic(polyline, polylineSymbol);
            graphicsOverlay.Graphics.Add(polylineGraphic);

            // Create a polygon graphic and add it to the graphics overlay.
            points = new MapPoint[4]
            {
                new MapPoint(-222e3, 6_558e3, SpatialReferences.WebMercator),
                new MapPoint(-228e3, 6_558e3, SpatialReferences.WebMercator),
                new MapPoint(-228e3, 6_555e3, SpatialReferences.WebMercator),
                new MapPoint(-222e3, 6_555e3, SpatialReferences.WebMercator)
            };
            var polygon = new Polygon(points, SpatialReferences.WebMercator);
            var polygonGraphic = new Graphic(polygon, polygonSymbol);
            graphicsOverlay.Graphics.Add(polygonGraphic);

            // Create a graphic with a picture marker symbol (image resource) and add it to the graphics overlay.
            var pinGraphic = await MakePictureMarkerSymbolFromImage(new MapPoint(-226_770, 6_550_470, SpatialReferences.WebMercator));
            graphicsOverlay.Graphics.Add(pinGraphic);

            // Create a graphic with a picture marker symbol (URL) and add it to the graphics overlay.
            var imageUri = new Uri("https://static.arcgis.com/images/Symbols/OutdoorRecreation/Camping.png");
            var campsiteSymbol = new PictureMarkerSymbol(imageUri)
            {
                Width = 25,
                Height = 25
            };
            var campsitePoint = new MapPoint(-223_560, 6_552_020, SpatialReferences.WebMercator);
            var campsiteGraphic = new Graphic(campsitePoint, campsiteSymbol);
            graphicsOverlay.Graphics.Add(campsiteGraphic);

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

        private async Task<Graphic> MakePictureMarkerSymbolFromImage(MapPoint point)
        {
            // Hold a reference to the picture marker symbol.
            PictureMarkerSymbol pinSymbol;

            // Get current assembly that contains the image.
            Assembly currentAssembly = Assembly.GetExecutingAssembly();

            // Get the resource name of the blue pin star image
            string resourceStreamName = this.GetType().Assembly.GetManifestResourceNames().Single(str => str.EndsWith("pin_star_blue.png"));

            // Load the resource stream
            using (Stream resourceStream = this.GetType().Assembly.GetManifestResourceStream(resourceStreamName))
            {
                // Create new symbol using asynchronous factory method from stream.
                pinSymbol = await PictureMarkerSymbol.CreateAsync(resourceStream);
                pinSymbol.Width = 60;
                pinSymbol.Height = 60;
                // The image is a pin; offset the image so that the pinpoint is on the point rather than the image's true center.
                pinSymbol.LeaderOffsetX = 30;
                pinSymbol.OffsetY = 14;
            }

            return new Graphic(point, pinSymbol);
        }

        #region UI event handlers

        private void GeometryTypeTabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // Get the tab control.
            TabControl tabControl = sender as TabControl;

            // Update the selected graphic based on the selected tab.
            switch (tabControl.SelectedIndex)
            {
                // Point tab
                case 0:
                    _selectedGraphic = MyMapView.GraphicsOverlays[0].Graphics[0];
                    break;

                // Polyline tab
                case 1:
                    _selectedGraphic = MyMapView.GraphicsOverlays[0].Graphics[1];
                    break;

                // Polygon tab
                case 2:
                    _selectedGraphic = MyMapView.GraphicsOverlays[0].Graphics[2];
                    break;
            }
        }

        private void StyleComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // Selected graphic will be null when initializing the UI.
            if (_selectedGraphic == null) return;

            // Get the selected combo box item.
            var comboBox = sender as System.Windows.Controls.ComboBox;

            // Update the symbol style based on the selected combo box item.
            switch (_selectedGraphic.Geometry.GeometryType)
            {
                case GeometryType.Point:
                    ((SimpleMarkerSymbol)_selectedGraphic.Symbol).Style = (SimpleMarkerSymbolStyle)comboBox.SelectedItem;
                    break;

                case GeometryType.Polyline:
                    ((SimpleLineSymbol)_selectedGraphic.Symbol).Style = (SimpleLineSymbolStyle)comboBox.SelectedItem;
                    break;

                case GeometryType.Polygon:
                    var symbol = (SimpleFillSymbol)_selectedGraphic.Symbol;
                    if (_stylingPolygonFill)
                    {
                        symbol.Style = (SimpleFillSymbolStyle)comboBox.SelectedItem;
                    }
                    else
                    {
                        symbol.Outline = new SimpleLineSymbol((SimpleLineSymbolStyle)comboBox.SelectedItem, symbol.Outline.Color, symbol.Outline.Width);
                    }
                    break;
            }
        }

        private void ColorDialogButton_Click(object sender, RoutedEventArgs e)
        {
            // Get the color preview border.
            var border = (sender as Button).Tag as Border;

            // Create a color dialog with the initial color reflecting the current symbol color.
            var colorDialog = new ColorDialog()
            {
                Color = Viewer.Converters.ColorToSolidBrushConverter.ConvertBack((SolidColorBrush)border.Background)
            };

            // Show the color dialog.
            if (colorDialog.ShowDialog() == DialogResult.OK)
            {
                border.Background = Viewer.Converters.ColorToSolidBrushConverter.Convert(colorDialog.Color);

                // Update the symbol color based on the selected geometry type.
                switch (_selectedGraphic.Geometry.GeometryType)
                {
                    case GeometryType.Point:
                        ((SimpleMarkerSymbol)_selectedGraphic.Symbol).Color = colorDialog.Color;
                        break;

                    case GeometryType.Polyline:
                        ((SimpleLineSymbol)_selectedGraphic.Symbol).Color = colorDialog.Color;
                        break;

                    case GeometryType.Polygon:
                        var symbol = (SimpleFillSymbol)_selectedGraphic.Symbol;
                        if (_stylingPolygonFill)
                        {
                            symbol.Color = colorDialog.Color;
                        }
                        else
                        {
                            symbol.Outline.Color = colorDialog.Color;
                        }
                        break;
                }
            }
        }

        private void SizeSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            // Selected graphic will be null when initializing the UI.
            if (_selectedGraphic == null) return;

            // Update the symbol size based on the selected geometry type.
            switch (_selectedGraphic.Geometry.GeometryType)
            {
                case GeometryType.Point:
                    ((SimpleMarkerSymbol)_selectedGraphic.Symbol).Size = e.NewValue;
                    break;

                case GeometryType.Polyline:
                    ((SimpleLineSymbol)_selectedGraphic.Symbol).Width = e.NewValue;
                    break;

                case GeometryType.Polygon:
                    ((SimpleFillSymbol)_selectedGraphic.Symbol).Outline.Width = e.NewValue;
                    break;
            }
        }

        #region Polygon styling

        private void PolygonFillStyleComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            _stylingPolygonFill = true;
            StyleComboBox_SelectionChanged(sender, e);
        }

        private void PolygonOutlineStyleComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            _stylingPolygonFill = false;
            StyleComboBox_SelectionChanged(sender, e);
        }

        private void PolygonFillColorDialogButton_Click(object sender, RoutedEventArgs e)
        {
            _stylingPolygonFill = true;
            ColorDialogButton_Click(sender, e);
        }

        private void PolygonOutlineColorDialogButton_Click(object sender, RoutedEventArgs e)
        {
            _stylingPolygonFill = false;
            ColorDialogButton_Click(sender, e);
        }

        #endregion Polygon styling

        #endregion UI event handlers
    }
}

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