Add graphics with renderer

View inC++QMLView on GitHubSample viewer app

A renderer allows you to change the style of all graphics in a graphics overlay by referencing a single symbol style. A renderer will only affect graphics that do not specify their own symbol style.

screenshot

Use case

A renderer allows you to change the style of all graphics in an overlay by only changing one copy of the symbol. For example, a user may wish to display a number of graphics on a map of parkland which represent trees, all sharing a common symbol.

How to use the sample

Pan and zoom on the map to view graphics for points, lines, and polygons (including polygons with curve segments), which are stylized using renderers.

How it works

  1. Create a GraphicsOverlay and add it to the MapView.
  2. Create a Graphic, specifying only a Geometry.
  3. Create a single Symbol such as a SimpleMarkerSymbol.
  4. Create a Renderer with the Symbol.
  5. Set the renderer on the GraphicsOverlay.

Relevant API

  • CubicBezierSegment
  • EllipticArcSegment
  • GeodesicEllipseParameters
  • Geometry
  • GeometryEngine
  • Graphic
  • GraphicsOverlay
  • PolygonBuilder
  • PolylineBulder
  • SimpleFillSymbol
  • SimpleLineSymbol
  • SimpleMarkerSymbol
  • SimpleRenderer

Additional information

To set unique symbols across a number of graphics (e.g. showing graphics of individual landmarks) see "Add graphics with symbols" sample.

Tags

arc, bezier, curve, display, ellipse, graphics, marker, overlay, renderer, segment, symbol, true curve

Sample Code

AddGraphicsWithRenderer.qml
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
// [WriteFile Name=AddGraphicsWithRenderer, Category=DisplayInformation]
// [Legal]
// Copyright 2016 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.
// [Legal]

import QtQuick 2.6
import QtQuick.Controls 2.2
import Esri.ArcGISRuntime 100.15

Rectangle {
    width: 800
    height: 600

    property var rtCreate: ArcGISRuntimeEnvironment.createObject

    // Map view UI presentation at top
    MapView {
        id: mapView
        anchors.fill: parent

        Component.onCompleted: {
            // Set the focus on MapView to initially enable keyboard navigation
            forceActiveFocus();
        }

        Map {
            Basemap {
                initStyle: Enums.BasemapStyleArcGISTopographic
            }
        }

        GraphicsOverlay {
            id: graphicsOverlay
        }

        Graphic {
            id: pointGraphic

            // A point graphic's geometry can be defined declaratively
            geometry: Point {
                x: 40e5
                y: 40e5
                spatialReference: SpatialReference {
                    wkid: 102100
                }
            }

            symbol: SimpleMarkerSymbol {
                style: Enums.SimpleMarkerSymbolStyleDiamond
                color: "red"
                size: 10
            }
        }

        Graphic {
            id: lineGraphic
            symbol: SimpleLineSymbol {
                style: Enums.SimpleLineSymbolStyleSolid
                color: "blue"
                width: 5
                antiAlias: true
            }
        }

        Graphic {
            id: polygonGraphic
            symbol: SimpleFillSymbol {
                style: Enums.SimpleFillSymbolStyleSolid
                color: Qt.rgba(1, 1, 0, 0.7)
            }
        }

        Graphic {
            id: ellipseGraphic
            symbol: SimpleFillSymbol {
                style: Enums.SimpleFillSymbolStyleSolid
                color: "purple"
            }
        }

        Graphic {
            id: heartGraphic
            symbol: SimpleFillSymbol {
                style: Enums.SimpleFillSymbolStyleSolid
                color: "red"
                outline: SimpleLineSymbol {
                    style: Enums.SimpleLineSymbolStyleSolid
                    color: "black"
                    width: 1
                }
            }
        }

        // Used to construct the line graphic geometry
        PolylineBuilder {
            id: polylineBuilder
            spatialReference: SpatialReference {
                wkid: 102100
            }
        }

        // Used to construct the polygon graphic geometry
        PolygonBuilder {
            id: polygonBuilder
            spatialReference: SpatialReference {
                wkid: 102100
            }
        }

        // Used to define the ellipse geometry
        GeodesicEllipseParameters {
            id: geodesicEllipseParameters
            center: Point {
                x: 40e5
                y: 25e5
                spatialReference: Factory.SpatialReference.createWebMercator();
            }
            geometryType: Enums.GeometryTypePolygon
            semiAxis1Length: 200
            semiAxis2Length: 400
            axisDirection: -45
            maxPointCount: 100
            angularUnit: AngularUnit {
                angularUnitId: Enums.AngularUnitIdDegrees
            }
            linearUnit: LinearUnit {
                linearUnitId: Enums.LinearUnitIdKilometers
            }
            maxSegmentLength: 20
        }
    }

    Component.onCompleted: {
        addPointGraphic();
        addLineGraphic();
        addPolygonGraphic();
        addHeartGraphic();
        addEllipseGraphic();
    }

    function addPointGraphic() {
        graphicsOverlay.graphics.append(pointGraphic);
    }

    function addLineGraphic() {
        // create the line by assigning points
        polylineBuilder.addPointXY(-10e5, 40e5);
        polylineBuilder.addPointXY(20e5, 50e5);
        // assign the graphics geometry to the line
        lineGraphic.geometry = polylineBuilder.geometry;
        // add the graphic to the polyline graphic overlay
        graphicsOverlay.graphics.append(lineGraphic);
    }

    function addPolygonGraphic() {
        // create the polygon by assigning points
        polygonBuilder.addPointXY(-20e5, 20e5);
        polygonBuilder.addPointXY(20e5, 20e5);
        polygonBuilder.addPointXY(20e5, -20e5);
        polygonBuilder.addPointXY(-20e5, -20e5);
        // assign the geometry of the graphic to be the polygon
        polygonGraphic.geometry = polygonBuilder.geometry;
        // add the graphic to the polygon graphics overlay
        graphicsOverlay.graphics.append(polygonGraphic);
    }

    function addEllipseGraphic() {
        // Create Ellipse
        ellipseGraphic.geometry = GeometryEngine.ellipseGeodesic(geodesicEllipseParameters);
        graphicsOverlay.graphics.append(ellipseGraphic);
    }

    function addHeartGraphic() {
        heartGraphic.geometry = createHeartGeometry(40e5, 5e5, 10e5, Factory.SpatialReference.createWebMercator());
        graphicsOverlay.graphics.append(heartGraphic);
    }

    function createHeartGeometry(centerX, centerY, sideLength, sr) {
        const minX = centerX - 0.5 * sideLength;
        const minY = centerY - 0.5 * sideLength;
        const arcRadius = sideLength * 0.25;

        // Variables used for arcs
        const rotationAngle = 0;
        const isMinor = false;
        const isCounterClockwise = false;
        const semiMajorAxis = sideLength * 0.25;
        const minorMajorRatio = 1;

        // Bottom left curve
        const leftCurveStart = rtCreate("Point", {x: centerX, y: minY, spatialReference: sr});
        const leftControlPoint1 = rtCreate("Point", {x: centerX, y: minY + 0.25 * sideLength, spatialReference: sr});
        const leftControlPoint2 = rtCreate("Point", {x: minX, y: centerY, spatialReference: sr});
        const leftCurveEnd = rtCreate("Point", {x: minX, y: minY + 0.75 * sideLength, spatialReference: sr});

        const leftCurveSegment = Factory.CubicBezierSegment.createWithPoints(
                                   leftCurveStart, leftControlPoint1, leftControlPoint2, leftCurveEnd, sr);

        // Top left arc
        const leftArcStart = leftCurveEnd;
        const leftArcEnd = rtCreate("Point", {x: centerX, y: minY + 0.75 * sideLength, spatialReference: sr});

        const leftArc = Factory.EllipticArcSegment.createEllipticArcWithStartAndEndpoints(
                          leftArcStart, leftArcEnd, rotationAngle, isMinor, isCounterClockwise, semiMajorAxis, minorMajorRatio, sr);

        // Top right arc
        const rightArcStart = leftArcEnd;
        const rightArcEnd = rtCreate("Point", {x: minX + sideLength, y: minY + 0.75 * sideLength, spatialReference: sr});

        const rightArc = Factory.EllipticArcSegment.createEllipticArcWithStartAndEndpoints(
                           rightArcStart, rightArcEnd, rotationAngle, isMinor, isCounterClockwise, semiMajorAxis, minorMajorRatio, sr);

        // Bottom right curve
        const rightCurveStart = rightArcEnd;
        const rightControlPoint1 = rtCreate("Point", {x: minX + sideLength, y: centerY, spatialReference: sr});
        const rightControlPoint2 = leftControlPoint1;
        const rightCurveEnd = leftCurveStart;

        const rightCurveSegment = Factory.CubicBezierSegment.createWithPoints(
                                    rightCurveStart, rightControlPoint1, rightControlPoint2, rightCurveEnd, sr);

        // Create a part with each of the segments
        const part = rtCreate("Part", {spatialReference: sr});
        part.addSegment(leftCurveSegment);
        part.addSegment(leftArc);
        part.addSegment(rightArc);
        part.addSegment(rightCurveSegment);

        const polygonBuilder = rtCreate("PolygonBuilder", {spatialReference: sr});
        polygonBuilder.parts.addPart(part);

        return polygonBuilder.geometry;
    }
}

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