Add graphics with renderer

View inQMLC++
View on GitHub
Sample 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 such as new SimpleRenderer(symbol).
  5. Set the renderer on the GraphicsOverlay with graphicsOverlay::setRenderer(renderer).

Relevant API

  • CubicBezierSegment
  • EllipticArcSegment
  • GeodesicEllipseParameters
  • Geometry
  • GeometryEngine
  • Graphic
  • GraphicsOverlay
  • PolygonBuilder
  • PolylineBuilder
  • 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.cppAddGraphicsWithRenderer.cppAddGraphicsWithRenderer.hAddGraphicsWithRenderer.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
// [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]

#ifdef PCH_BUILD
#include "pch.hpp"
#endif // PCH_BUILD

#include "AddGraphicsWithRenderer.h"

#include "CubicBezierSegment.h"
#include "EllipticArcSegment.h"
#include "GeodesicEllipseParameters.h"
#include "GeometryEngine.h"
#include "Map.h"
#include "MapQuickView.h"
#include "PolygonBuilder.h"
#include "PolylineBuilder.h"
#include "SimpleFillSymbol.h"
#include "SimpleLineSymbol.h"
#include "SimpleMarkerSymbol.h"
#include "SimpleRenderer.h"

using namespace Esri::ArcGISRuntime;

AddGraphicsWithRenderer::AddGraphicsWithRenderer(QQuickItem* parent) :
  QQuickItem(parent)
{
}

AddGraphicsWithRenderer::~AddGraphicsWithRenderer() = default;

void AddGraphicsWithRenderer::init()
{
  qmlRegisterType<MapQuickView>("Esri.Samples", 1, 0, "MapView");
  qmlRegisterType<AddGraphicsWithRenderer>("Esri.Samples", 1, 0, "AddGraphicsWithRendererSample");
}

void AddGraphicsWithRenderer::componentComplete()
{
  QQuickItem::componentComplete();

  // find QML MapView component
  m_mapView = findChild<MapQuickView*>("mapView");

  // Create a map using the topo basemap
  m_map = new Map(BasemapStyle::ArcGISTopographic, this);
  // set map on the map view
  m_mapView->setMap(m_map);

  addGraphicsOverlays();
}

void AddGraphicsWithRenderer::addGraphicsOverlays()
{
  addPointGraphic();

  addLineGraphic();

  addPolygonGraphic();

  addCurveGraphic();

  addEllipseGraphic();
}

void AddGraphicsWithRenderer::addPointGraphic()
{
  const Point pointGeometry(40e5, 40e5, SpatialReference::webMercator());

  SimpleMarkerSymbol* pointSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbolStyle::Diamond, QColor("red"), 10, this);

  Graphic* pointGraphic = new Graphic(pointGeometry, this);

  createGraphicsOverlayWithGraphicAndSymbol(pointGraphic, pointSymbol);
}

void AddGraphicsWithRenderer::addLineGraphic()
{
  PolygonBuilder polylineBuilder(SpatialReference::webMercator());
  polylineBuilder.addPoint(-10e5, 40e5);
  polylineBuilder.addPoint(20e5, 50e5);

  SimpleLineSymbol* lineSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle::Solid, QColor("blue"), 5, this);

  Graphic* lineGraphic = new Graphic(polylineBuilder.toGeometry(), this);

  createGraphicsOverlayWithGraphicAndSymbol(lineGraphic, lineSymbol);
}

void AddGraphicsWithRenderer::addPolygonGraphic()
{
  PolygonBuilder polygonBuilder(SpatialReference::webMercator());
  polygonBuilder.addPoint(-20e5, 20e5);
  polygonBuilder.addPoint(20e5, 20e5);
  polygonBuilder.addPoint(20e5, -20e5);
  polygonBuilder.addPoint(-20e5, -20e5);

  SimpleFillSymbol* polygonFillSymbol = new SimpleFillSymbol(SimpleFillSymbolStyle::Solid, QColor(255, 255, 0, 180), this);

  Graphic* polygonGraphic = new Graphic(polygonBuilder.toGeometry(), this);

  createGraphicsOverlayWithGraphicAndSymbol(polygonGraphic, polygonFillSymbol);
}

void AddGraphicsWithRenderer::addCurveGraphic()
{
  Geometry heartShapedCurve = createHeart();

  Graphic* curveGraphic = new Graphic(heartShapedCurve, this);

  SimpleLineSymbol* curveLineSymbol = new SimpleLineSymbol(SimpleLineSymbolStyle::Solid, QColor("black"), 1, this);
  SimpleFillSymbol* curveFillSymbol = new SimpleFillSymbol(SimpleFillSymbolStyle::Solid, QColor("red"), curveLineSymbol, this);

  createGraphicsOverlayWithGraphicAndSymbol(curveGraphic, curveFillSymbol);
}

void AddGraphicsWithRenderer::addEllipseGraphic()
{
  GeodesicEllipseParameters parameters = GeodesicEllipseParameters();
  parameters.setCenter(Point(40e5, 25e5, SpatialReference::webMercator()));
  parameters.setGeometryType(GeometryType::Polygon);
  parameters.setSemiAxis1Length(200);
  parameters.setSemiAxis2Length(400);
  parameters.setAxisDirection(-45);
  parameters.setMaxPointCount(100);
  parameters.setAngularUnit(AngularUnit(AngularUnitId::Degrees));
  parameters.setLinearUnit(LinearUnit(LinearUnitId::Kilometers));
  parameters.setMaxSegmentLength(20);

  const Polygon ellipsePolygon = GeometryEngine::ellipseGeodesic(parameters);

  SimpleFillSymbol* ellipseSymbol = new SimpleFillSymbol(SimpleFillSymbolStyle::Solid, QColor(125, 0, 125), this);

  createGraphicsOverlayWithGraphicAndSymbol(new Graphic(ellipsePolygon, this), ellipseSymbol);
}

void AddGraphicsWithRenderer::createGraphicsOverlayWithGraphicAndSymbol(Graphic* graphic, Symbol* symbol)
{
  GraphicsOverlay* graphicOverlay = new GraphicsOverlay(this);
  // set the renderer of the overlay to be the symbol
  graphicOverlay->setRenderer(new SimpleRenderer(symbol, this));
  // add the graphic to the overlay
  graphicOverlay->graphics()->append(graphic);
  // add the overlay to the mapview
  m_mapView->graphicsOverlays()->append(graphicOverlay);
}

Geometry AddGraphicsWithRenderer::createHeart()
{
  // Declare common variables
  const Point origin(40e5, 5e5, 0, SpatialReference::webMercator());
  SpatialReference spatialReference = origin.spatialReference();
  int sideLength = 10e5;
  int minX = origin.x() - 0.5 * sideLength;
  int minY = origin.y() - 0.5 * sideLength;
  int arcRadius = sideLength * 0.25;

  // The heart-shape comprises 4-curve segments: 2 bezier curves make the base and 2 arc curves make the top
  // Declare all points used to create heart-shaped curve, starting at bottom tip and moving clockwise
  const Point bottomTip(origin.x(), minY, spatialReference);
  const Point leftControlPoint1(origin.x(), minY + 0.25 * sideLength, spatialReference);
  const Point leftControlPoint2(minX, origin.y(), spatialReference);
  const Point leftCurveEnd(minX, minY + 0.75 * sideLength, spatialReference);
  const Point leftArcCentre(minX + 0.25 * sideLength, minY + 0.75 * sideLength, spatialReference);
  const Point arcIntersection(origin.x(), minY + 0.75 * sideLength, spatialReference);
  const Point rightArcCentre(minX + sideLength, minY + 0.75 * sideLength, spatialReference);
  const Point rightCurveStart(minX + sideLength, minY + 0.75 * sideLength, spatialReference);
  const Point rightControlPoint1(minX + sideLength, origin.y(), spatialReference);
  const Point rightControlPoint2 = leftControlPoint1;

  // Create curve segments
  CubicBezierSegment leftCurve(bottomTip, leftControlPoint1, leftControlPoint2, leftCurveEnd, spatialReference);
  EllipticArcSegment leftArc(leftCurveEnd, arcIntersection, 0, false, false, arcRadius, 1, spatialReference);
  EllipticArcSegment rightArc(arcIntersection, rightCurveStart, 0, false, false, arcRadius, 1, spatialReference);
  CubicBezierSegment rightCurve(rightCurveStart, rightControlPoint1, rightControlPoint2, bottomTip, spatialReference);

  // Create part from segments
  Part* heart = new Part(spatialReference, this);
  heart->addSegment(leftCurve);
  heart->addSegment(leftArc);
  heart->addSegment(rightArc);
  heart->addSegment(rightCurve);

  // Create part collection
  PartCollection* partCollection = new PartCollection(spatialReference, this);
  partCollection->addPart(heart);

  // Construct polygon from part collection
  PolygonBuilder* heartBuilder = new PolygonBuilder(origin.spatialReference(), this);
  heartBuilder->setParts(partCollection);

  return heartBuilder->toGeometry();
}

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