Skip to content

Style graphics with renderer

View on GitHub

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 of style graphics with renderer sample

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 Graphic, specifying only a Geometry.
  2. Create a GraphicsOverlay with the graphic.
  3. Create a Symbol such as a SimpleMarkerSymbol.
  4. Create a renderer with SimpleRenderer(symbol:), passing in a Symbol.
  5. Set the renderer for the graphics overlay.

Relevant API

  • CubicBezierSegment
  • EllipticArcSegment
  • GeodesicEllipseParameters
  • Geometry
  • Graphic
  • GraphicsOverlay
  • MutablePart
  • Polygon
  • Polyline
  • SimpleFillSymbol
  • SimpleLineSymbol
  • SimpleMarkerSymbol
  • SimpleRenderer
  • static GeometryEngine.geodesicEllipse(parameters:)

Additional information

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

Tags

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

Sample Code

StyleGraphicsWithRendererView.swift
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 // 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 // // https://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.  import ArcGIS import SwiftUI  struct StyleGraphicsWithRendererView: View {  /// The view model for the sample.  @StateObject private var model = Model()   var body: some View {  MapView(map: model.map, graphicsOverlays: model.graphicsOverlays)  } }  private extension StyleGraphicsWithRendererView {  /// The model used to store the geo model and other expensive objects  /// used in this view.  class Model: ObservableObject {  /// A map with a topographic basemap style and an initial viewpoint.  let map: Map = {  let map = Map(basemapStyle: .arcGISTopographic)  map.initialViewpoint = Viewpoint(center: Point(x: 20e5, y: 20e5, spatialReference: .webMercator), scale: 7e7)  return map  }()   /// The graphics overlays used in this sample.  var graphicsOverlays: [GraphicsOverlay] {  return [  pointGraphicsOverlay,  lineGraphicsOverlay,  squarePolygonGraphicsOverlay,  ellipseGraphicsOverlay,  curvedPolygonGraphicsOverlay  ]  }   /// The graphics overlay rendering a point.  private let pointGraphicsOverlay: GraphicsOverlay = {  // Creates a simple marker symbol.  let symbol = SimpleMarkerSymbol(style: .diamond, color: .green, size: 10)  // Creates the geometry for the point.  let geometry = Point(x: 40e5, y: 40e5, spatialReference: .webMercator)   // Creates a graphics overlay containing a graphic with the point geometry.  let overlay = GraphicsOverlay(graphics: [Graphic(geometry: geometry)])  // Creates and assigns a simple renderer to the graphics overlay.  overlay.renderer = SimpleRenderer(symbol: symbol)  return overlay  }()   /// The graphics overlay rendering a line.  private let lineGraphicsOverlay: GraphicsOverlay = {  // Creates a simple line symbol.  let symbol = SimpleLineSymbol(style: .solid, color: .blue, width: 5)  // Creates the geometry for the line.  let geometry = Polyline(  points: [  Point(x: -10e5, y: 40e5),  Point(x: 20e5, y: 50e5)  ],  spatialReference: .webMercator  )   // Creates a graphics overlay containing a graphic with the line geometry.  let overlay = GraphicsOverlay(graphics: [Graphic(geometry: geometry)])  // Creates and assigns a simple renderer to the graphics overlay.  overlay.renderer = SimpleRenderer(symbol: symbol)  return overlay  }()   /// The graphics overlay rendering a square polygon.  private let squarePolygonGraphicsOverlay: GraphicsOverlay = {  // Creates a simple fill symbol.  let symbol = SimpleFillSymbol(color: .yellow)  // Creates the geometry for the square polygon.  let geometry = Polygon(  points: [  Point(x: -20e5, y: 20e5),  Point(x: 20e5, y: 20e5),  Point(x: 20e5, y: -20e5),  Point(x: -20e5, y: -20e5)  ],  spatialReference: .webMercator  )   // Creates a graphics overlay containing a graphic with the square geometry.  let overlay = GraphicsOverlay(graphics: [Graphic(geometry: geometry)])  // Creates and assigns a simple renderer to the graphics overlay.  overlay.renderer = SimpleRenderer(symbol: symbol)  return overlay  }()   /// The graphics overlay rendering an ellipse.  private let ellipseGraphicsOverlay: GraphicsOverlay = {  // Creates a simple fill symbol.  let symbol = SimpleFillSymbol(color: .purple)  // Defines the center point of the ellipse.  let center = Point(x: 40e5, y: 25e5, spatialReference: .webMercator)  // Creates the parameters for the ellipse.  let parameters = GeodesicEllipseParameters<ArcGIS.Polygon>(  axisDirection: -45,  center: center,  linearUnit: .kilometers,  maxPointCount: 100,  maxSegmentLength: 20,  semiAxis1Length: 200,  semiAxis2Length: 400  )  // Creates the geometry for the ellipse from the parameters.  let geometry = GeometryEngine.geodesicEllipse(parameters: parameters)   // Creates a graphics overlay containing a graphic with the ellipse geometry.  let overlay = GraphicsOverlay(graphics: [Graphic(geometry: geometry)])  // Creates and assigns a simple renderer to the graphics overlay.  overlay.renderer = SimpleRenderer(symbol: symbol)  return overlay  }()   /// The graphics overlay rendering a curved polygon.  private let curvedPolygonGraphicsOverlay: GraphicsOverlay = {  // Creates a simple fill symbol with an outline.  let lineSymbol = SimpleLineSymbol(style: .solid, color: .black, width: 1)  let fillSymbol = SimpleFillSymbol(color: .red, outline: lineSymbol)  // Defines the point of origin for the curved polygon.  let origin = Point(x: 40e5, y: 5e5, spatialReference: .webMercator)  // Creates a heart-shaped polygon.  let heartPolygon = makeHeartPolygon(center: origin, sideLength: 10e5)   // Creates a graphics overlay containing a graphic with the polygon geometry.  let overlay = GraphicsOverlay(graphics: [Graphic(geometry: heartPolygon)])  // Creates and assigns a simple renderer to the graphics overlay.  overlay.renderer = SimpleRenderer(symbol: fillSymbol)  return overlay  }()   /// Creates a heart-shaped polygon with Bezier and elliptic arc segments.  /// - Parameters:  /// - center: The center of the square that contains the heart shape.  /// - sideLength: The side length of the square.  /// - Returns: A heart-shaped polygon.  private static func makeHeartPolygon(center: Point, sideLength: Double) -> ArcGIS.Polygon? {  guard sideLength > 0 else { return nil }  let spatialReference = center.spatialReference  // Defines the x and y coordinates to simplify the calculation.  let minX = center.x - sideLength * 0.5  let minY = center.y - sideLength * 0.5  // Defines the radius of the arcs.  let arcRadius = sideLength * 0.25   // Creates the bottom left curve segment.  let leftCurveStart = Point(x: center.x, y: minY, spatialReference: spatialReference)  let leftCurveEnd = Point(x: minX, y: minY + sideLength * 0.75, spatialReference: spatialReference)  let leftControlPoint1 = Point(x: center.x, y: minY + sideLength * 0.25, spatialReference: spatialReference)  let leftControlPoint2 = Point(x: minX, y: center.y, spatialReference: spatialReference)  let leftCurve = CubicBezierSegment(  startPoint: leftCurveStart,  controlPoint1: leftControlPoint1,  controlPoint2: leftControlPoint2,  endPoint: leftCurveEnd,  spatialReference: spatialReference  )   // Creates the top left arc segment.  let leftArcCenter = Point(x: minX + sideLength * 0.25, y: minY + sideLength * 0.75, spatialReference: spatialReference)  let leftArc = EllipticArcSegment.makeCircular(  centerPoint: leftArcCenter,  radius: arcRadius,  startAngle: .pi,  centralAngle: -.pi,  spatialReference: spatialReference  )   // Creates the top right arc segment.  let rightArcCenter = Point(x: minX + sideLength * 0.75, y: minY + sideLength * 0.75, spatialReference: spatialReference)  let rightArc = EllipticArcSegment.makeCircular(  centerPoint: rightArcCenter,  radius: arcRadius,  startAngle: .pi,  centralAngle: -.pi,  spatialReference: spatialReference  )   // Creates the bottom right curve segment.  let rightCurveStart = Point(x: minX + sideLength, y: minY + sideLength * 0.75, spatialReference: spatialReference)  let rightCurveEnd = leftCurveStart  let rightControlPoint1 = Point(x: minX + sideLength, y: center.y, spatialReference: spatialReference)  let rightControlPoint2 = leftControlPoint1  let rightCurve = CubicBezierSegment(  startPoint: rightCurveStart,  controlPoint1: rightControlPoint1,  controlPoint2: rightControlPoint2,  endPoint: rightCurveEnd,  spatialReference: spatialReference  )   // Creates and returns the heart polygon.  return Polygon(parts: [MutablePart(segments: [leftCurve, leftArc, rightArc, rightCurve], spatialReference: spatialReference)])  }  } }  #Preview {  StyleGraphicsWithRendererView() }

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