Style graphics with symbols

View on GitHubSample viewer app

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

Image of Style 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

Pan and zoom around the map. 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

MainActivity.kt
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 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.
 *
 */

package com.esri.arcgismaps.sample.stylegraphicswithsymbols

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import com.arcgismaps.ApiKey
import com.arcgismaps.ArcGISEnvironment
import com.arcgismaps.Color
import com.arcgismaps.geometry.Point
import com.arcgismaps.geometry.Polygon
import com.arcgismaps.geometry.PolygonBuilder
import com.arcgismaps.geometry.Polyline
import com.arcgismaps.geometry.PolylineBuilder
import com.arcgismaps.geometry.SpatialReference
import com.arcgismaps.mapping.ArcGISMap
import com.arcgismaps.mapping.BasemapStyle
import com.arcgismaps.mapping.Viewpoint
import com.arcgismaps.mapping.symbology.HorizontalAlignment
import com.arcgismaps.mapping.symbology.SimpleFillSymbol
import com.arcgismaps.mapping.symbology.SimpleFillSymbolStyle
import com.arcgismaps.mapping.symbology.SimpleLineSymbol
import com.arcgismaps.mapping.symbology.SimpleLineSymbolStyle
import com.arcgismaps.mapping.symbology.SimpleMarkerSymbol
import com.arcgismaps.mapping.symbology.SimpleMarkerSymbolStyle
import com.arcgismaps.mapping.symbology.TextSymbol
import com.arcgismaps.mapping.symbology.VerticalAlignment
import com.arcgismaps.mapping.view.Graphic
import com.arcgismaps.mapping.view.GraphicsOverlay
import com.esri.arcgismaps.sample.stylegraphicswithsymbols.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // authentication with an API key or named user is
        // required to access basemaps and other location services
        ArcGISEnvironment.apiKey = ApiKey.create(BuildConfig.API_KEY)

        // set up data binding for the activity
        val activityMainBinding: ActivityMainBinding =
            DataBindingUtil.setContentView(this, R.layout.activity_main)
        val mapView = activityMainBinding.mapView
        lifecycle.addObserver(mapView)

        // create the graphics overlay
        val graphicsOverlay = GraphicsOverlay()

        mapView.apply {
            // create a map with the BasemapStyle Oceans and display it in this view
            map = ArcGISMap(BasemapStyle.ArcGISOceans)
            setViewpoint(Viewpoint(56.075844, -2.681572, 100000.0))
            // add the overlay to the map view
            graphicsOverlays.add(graphicsOverlay)
        }

        // add some buoy positions to the graphics overlay
        val buoyPoints = createBuoyGraphics()
        graphicsOverlay.graphics.addAll(buoyPoints)

        // add boat trip polyline to graphics overlay
        val tripRouteGraphic = createRoute()
        graphicsOverlay.graphics.add(tripRouteGraphic)

        // add nesting ground polygon to graphics overlay
        val nestingGround = createNestingGround()
        graphicsOverlay.graphics.add(nestingGround)

        // add text symbols and points to graphics overlay
        val textGraphics = createTextGraphics()
        graphicsOverlay.graphics.addAll(textGraphics)
    }

    /**
     * Create Graphics for some points.
     *
     * @return a new graphic
     */
    private fun createBuoyGraphics(): Array<Graphic> {
        // define the buoy locations
        val buoy1Loc = Point(-2.712642647560347, 56.06281256681154, SpatialReference.wgs84())
        val buoy2Loc = Point(-2.690841695957230, 56.06444173689877, SpatialReference.wgs84())
        val buoy3Loc = Point(-2.669727388499094, 56.06425007340287, SpatialReference.wgs84())
        val buoy4Loc = Point(-2.639515046119973, 56.06127916736989, SpatialReference.wgs84())

        // create a marker symbol
        val buoyMarker =
            SimpleMarkerSymbol(SimpleMarkerSymbolStyle.Circle, Color.red, 10.0f)

        // create graphics
        return arrayOf(
            Graphic(buoy1Loc, buoyMarker),
            Graphic(buoy2Loc, buoyMarker),
            Graphic(buoy3Loc, buoyMarker),
            Graphic(buoy4Loc, buoyMarker)
        )
    }

    /**
     * Create graphics which display text at specific locations.
     *
     * @return a new graphic
     */
    private fun createTextGraphics(): Array<Graphic> {
        // create a point geometry
        val bassLocation =
            Point(-2.640631, 56.078083, SpatialReference.wgs84())
        val craigleithLocation =
            Point(-2.720324, 56.073569, SpatialReference.wgs84())

        // create text symbols
        val bassRockSymbol = TextSymbol(
            getString(R.string.bassrock),
            Color.blue,
            10.0f,
            HorizontalAlignment.Left, VerticalAlignment.Bottom
        )
        val craigleithSymbol = TextSymbol(
            getString(R.string.craigleith),
            Color.blue,
            10.0f,
            HorizontalAlignment.Right, VerticalAlignment.Top
        )

        // define graphics from each geometry and symbol
        val bassRockGraphic = Graphic(bassLocation, bassRockSymbol)
        val craigleithGraphic = Graphic(craigleithLocation, craigleithSymbol)

        return arrayOf(bassRockGraphic, craigleithGraphic)
    }

    /**
     * Create a graphic which displays a polyline.
     *
     * @return a new graphic
     */
    private fun createRoute(): Graphic {
        // define a polyline for the boat trip
        val boatRoute: Polyline = getBoatTripGeometry()
        // define a line symbol
        val lineSymbol =
            SimpleLineSymbol(SimpleLineSymbolStyle.Dash, Color.magenta, 4.0f)

        // create and return a new graphic
        return Graphic(boatRoute, lineSymbol)
    }

    /**
     * Create a graphic which displays a polygon.
     *
     * @return a new graphic
     */
    private fun createNestingGround(): Graphic {
        // define the polygon for the nesting ground
        val nestingGround = getNestingGroundGeometry()
        // define the fill symbol and outline
        val outlineSymbol =
            SimpleLineSymbol(SimpleLineSymbolStyle.Dash, Color.blue, 1.0f)
        val fillSymbol = SimpleFillSymbol(
            SimpleFillSymbolStyle.DiagonalCross, Color.green,
            outlineSymbol
        )

        // create and return a new graphic
        return Graphic(nestingGround, fillSymbol)
    }

    /**
     * Create a polyline representing the route of the boat trip.
     *
     * @return a new polyline
     */
    private fun getBoatTripGeometry(): Polyline {
        // a new point collection to make up the polyline
        val boatPositionsPolylineBuilder = PolylineBuilder(SpatialReference.wgs84()) {
            // add positions to the point collection
            addPoint(Point(-2.718479122792677, 56.06147084563517))
            addPoint(Point(-2.719680750046392, 56.06147084563517))
            addPoint(Point(-2.722084004553823, 56.06214171205971))
            addPoint(Point(-2.726375530459948, 56.06386674355254))
            addPoint(Point(-2.726890513568683, 56.06607083814320))
            addPoint(Point(-2.727062174604927, 56.06779569383808))
            addPoint(Point(-2.725517225278723, 56.06875391365391))
            addPoint(Point(-2.723113970771293, 56.06942465335233))
            addPoint(Point(-2.719165766937657, 56.07028701581465))
            addPoint(Point(-2.713672613777817, 56.07057446568132))
            addPoint(Point(-2.709381087871692, 56.07095772883556))
            addPoint(Point(-2.704402917820587, 56.07153261642126))
            addPoint(Point(-2.698223120515766, 56.07239493172226))
            addPoint(Point(-2.692386645283435, 56.07325722773041))
            addPoint(Point(-2.686721831087350, 56.07335303720707))
            addPoint(Point(-2.681228677927500, 56.07354465544585))
            addPoint(Point(-2.676422168912640, 56.07421531177896))
            addPoint(Point(-2.669899049535339, 56.07488595644139))
            addPoint(Point(-2.664749218447989, 56.07574819671591))
            addPoint(Point(-2.659427726324393, 56.07613140842321))
            addPoint(Point(-2.654792878345778, 56.07622721075461))
            addPoint(Point(-2.651359657620878, 56.07651461631978))
            addPoint(Point(-2.647754775859732, 56.07708942101955))
            addPoint(Point(-2.645008199279812, 56.07814320736718))
            addPoint(Point(-2.643291588917362, 56.08025069360931))
            addPoint(Point(-2.638656740938747, 56.08044227755186))
            addPoint(Point(-2.636940130576297, 56.07881378367495))
            addPoint(Point(-2.636425147467562, 56.07728102068079))
            addPoint(Point(-2.637798435757522, 56.07661041769850))
            addPoint(Point(-2.638656740938747, 56.07507756705851))
            addPoint(Point(-2.641231656482422, 56.07479015077557))
            addPoint(Point(-2.642776605808628, 56.07574819671591))
            addPoint(Point(-2.645694843424792, 56.07546078543464))
            addPoint(Point(-2.647239792750997, 56.07459853872940))
            addPoint(Point(-2.649299725185938, 56.07268236586862))
            addPoint(Point(-2.653076267983328, 56.07182005699860))
            addPoint(Point(-2.655479522490758, 56.07086191340429))
            addPoint(Point(-2.658741082179413, 56.07047864929729))
            addPoint(Point(-2.663375930158029, 56.07028701581465))
            addPoint(Point(-2.666637489846684, 56.07009538137926))
            addPoint(Point(-2.670070710571584, 56.06990374599109))
            addPoint(Point(-2.674190575441464, 56.06913719491074))
            addPoint(Point(-2.678310440311345, 56.06808316228391))
            addPoint(Point(-2.682086983108735, 56.06789151689155))
            addPoint(Point(-2.686893492123596, 56.06760404701653))
            addPoint(Point(-2.691185018029721, 56.06722075051504))
            addPoint(Point(-2.695133221863356, 56.06702910083509))
            addPoint(Point(-2.698223120515766, 56.06683745020233))
            addPoint(Point(-2.701656341240667, 56.06645414607839))
            addPoint(Point(-2.706119528183037, 56.06607083814320))
            addPoint(Point(-2.710067732016672, 56.06559169786458))
            addPoint(Point(-2.713329291705327, 56.06520838135397))
            addPoint(Point(-2.716762512430227, 56.06453756828941))
            addPoint(Point(-2.718307461756433, 56.06348340989081))
            addPoint(Point(-2.719165766937657, 56.06281256681154))
            addPoint(Point(-2.719852411082638, 56.06204587471371))
            addPoint(Point(-2.719165766937657, 56.06166252294756))
            addPoint(Point(-2.718307461756433, 56.06147084563517))
        }

        // create the polyline from the point collection
        return boatPositionsPolylineBuilder.toGeometry()
    }

    /**
     * Create a polygon from a point collection.
     *
     * @return a new polygon
     */
    private fun getNestingGroundGeometry(): Polygon {
        // a new point collection to make up the polygon
        val pointsPolygonBuilder = PolygonBuilder(SpatialReference.wgs84()) {
            // add points to the point collection
            addPoint(Point(-2.643077012566659, 56.07712534604447))
            addPoint(Point(-2.642819521015944, 56.07717324600376))
            addPoint(Point(-2.642540571836003, 56.07774804087097))
            addPoint(Point(-2.642712232869812, 56.07792766250863))
            addPoint(Point(-2.642454741319098, 56.07829887790651))
            addPoint(Point(-2.641853927700763, 56.07852639525372))
            addPoint(Point(-2.640974164902487, 56.07880180919243))
            addPoint(Point(-2.639987113958079, 56.07881378366685))
            addPoint(Point(-2.639407757968971, 56.07908919555142))
            addPoint(Point(-2.638764029092183, 56.07917301616904))
            addPoint(Point(-2.638485079912242, 56.07896945149566))
            addPoint(Point(-2.638570910429147, 56.07820308072684))
            addPoint(Point(-2.638785486721410, 56.07756841839600))
            addPoint(Point(-2.639193181676709, 56.07719719596109))
            addPoint(Point(-2.639944198699627, 56.07675411934114))
            addPoint(Point(-2.640652300464093, 56.07673016910844))
            addPoint(Point(-2.640673758093319, 56.07632301287509))
            addPoint(Point(-2.640180232621116, 56.07599967986049))
            addPoint(Point(-2.640244605508794, 56.07584400003405))
            addPoint(Point(-2.640416266542604, 56.07578412301025))
            addPoint(Point(-2.640888334385582, 56.07580807383093))
            addPoint(Point(-2.641768097183858, 56.07623918605773))
            addPoint(Point(-2.642197249768383, 56.07625116132851))
            addPoint(Point(-2.642840978645171, 56.07661041772168))
            addPoint(Point(-2.643077012566659, 56.07712534604447))
        }

        // create a polygon from the point collection
        return pointsPolygonBuilder.toGeometry()
    }

    private val Color.Companion.blue: Color
        get() {
            return fromRgba(0, 0, 255, 255)
        }

    private val Color.Companion.magenta: Color
        get() {
            return fromRgba(255, 0, 255, 255)
        }
}

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