Add graphics with symbols

View inJavaKotlinView on GitHubSample viewer app

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

Image of Add 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
/*
 * Copyright 2020 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.arcgisruntime.sample.addgraphicswithsymbols

import android.graphics.Color
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.esri.arcgisruntime.ArcGISRuntimeEnvironment
import com.esri.arcgisruntime.geometry.*
import com.esri.arcgisruntime.mapping.ArcGISMap
import com.esri.arcgisruntime.mapping.BasemapStyle
import com.esri.arcgisruntime.mapping.Viewpoint
import com.esri.arcgisruntime.mapping.view.Graphic
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay
import com.esri.arcgisruntime.mapping.view.MapView
import com.esri.arcgisruntime.sample.addgraphicswithsymbols.databinding.ActivityMainBinding
import com.esri.arcgisruntime.symbology.SimpleFillSymbol
import com.esri.arcgisruntime.symbology.SimpleLineSymbol
import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol
import com.esri.arcgisruntime.symbology.TextSymbol

class MainActivity : AppCompatActivity() {

    private val activityMainBinding by lazy {
        ActivityMainBinding.inflate(layoutInflater)
    }

    private val mapView: MapView by lazy {
        activityMainBinding.mapView
    }

    private val wgs84 = SpatialReferences.getWgs84()

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

        // authentication with an API key or named user is required to access basemaps and other
        // location services
        ArcGISRuntimeEnvironment.setApiKey(BuildConfig.API_KEY)

        // 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.ARCGIS_OCEANS)
            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, wgs84)
        val buoy2Loc = Point(-2.690841695957230, 56.06444173689877, wgs84)
        val buoy3Loc = Point(-2.669727388499094, 56.06425007340287, wgs84)
        val buoy4Loc = Point(-2.639515046119973, 56.06127916736989, wgs84)

        // create a marker symbol
        val buoyMarker =
            SimpleMarkerSymbol(SimpleMarkerSymbol.Style.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, wgs84)
        val craigleithLocation =
            Point(-2.720324, 56.073569, wgs84)

        // create text symbols
        val bassRockSymbol = TextSymbol(
            10.0f, getString(R.string.bassrock), Color.rgb(0, 0, 230),
            TextSymbol.HorizontalAlignment.LEFT, TextSymbol.VerticalAlignment.BOTTOM
        )
        val craigleithSymbol = TextSymbol(
            10.0f, getString(R.string.craigleith), Color.rgb(0, 0, 230),
            TextSymbol.HorizontalAlignment.RIGHT, TextSymbol.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(SimpleLineSymbol.Style.DASH, Color.rgb(128, 0, 128), 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(SimpleLineSymbol.Style.DASH, Color.rgb(0, 0, 128), 1.0f)
        val fillSymbol = SimpleFillSymbol(
            SimpleFillSymbol.Style.DIAGONAL_CROSS, Color.rgb(0, 80, 0),
            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 boatPositions = PointCollection(wgs84).apply {
            // add positions to the point collection
            add(Point(-2.718479122792677, 56.06147084563517))
            add(Point(-2.719680750046392, 56.06147084563517))
            add(Point(-2.722084004553823, 56.06214171205971))
            add(Point(-2.726375530459948, 56.06386674355254))
            add(Point(-2.726890513568683, 56.06607083814320))
            add(Point(-2.727062174604927, 56.06779569383808))
            add(Point(-2.725517225278723, 56.06875391365391))
            add(Point(-2.723113970771293, 56.06942465335233))
            add(Point(-2.719165766937657, 56.07028701581465))
            add(Point(-2.713672613777817, 56.07057446568132))
            add(Point(-2.709381087871692, 56.07095772883556))
            add(Point(-2.704402917820587, 56.07153261642126))
            add(Point(-2.698223120515766, 56.07239493172226))
            add(Point(-2.692386645283435, 56.07325722773041))
            add(Point(-2.686721831087350, 56.07335303720707))
            add(Point(-2.681228677927500, 56.07354465544585))
            add(Point(-2.676422168912640, 56.07421531177896))
            add(Point(-2.669899049535339, 56.07488595644139))
            add(Point(-2.664749218447989, 56.07574819671591))
            add(Point(-2.659427726324393, 56.07613140842321))
            add(Point(-2.654792878345778, 56.07622721075461))
            add(Point(-2.651359657620878, 56.07651461631978))
            add(Point(-2.647754775859732, 56.07708942101955))
            add(Point(-2.645008199279812, 56.07814320736718))
            add(Point(-2.643291588917362, 56.08025069360931))
            add(Point(-2.638656740938747, 56.08044227755186))
            add(Point(-2.636940130576297, 56.07881378367495))
            add(Point(-2.636425147467562, 56.07728102068079))
            add(Point(-2.637798435757522, 56.07661041769850))
            add(Point(-2.638656740938747, 56.07507756705851))
            add(Point(-2.641231656482422, 56.07479015077557))
            add(Point(-2.642776605808628, 56.07574819671591))
            add(Point(-2.645694843424792, 56.07546078543464))
            add(Point(-2.647239792750997, 56.07459853872940))
            add(Point(-2.649299725185938, 56.07268236586862))
            add(Point(-2.653076267983328, 56.07182005699860))
            add(Point(-2.655479522490758, 56.07086191340429))
            add(Point(-2.658741082179413, 56.07047864929729))
            add(Point(-2.663375930158029, 56.07028701581465))
            add(Point(-2.666637489846684, 56.07009538137926))
            add(Point(-2.670070710571584, 56.06990374599109))
            add(Point(-2.674190575441464, 56.06913719491074))
            add(Point(-2.678310440311345, 56.06808316228391))
            add(Point(-2.682086983108735, 56.06789151689155))
            add(Point(-2.686893492123596, 56.06760404701653))
            add(Point(-2.691185018029721, 56.06722075051504))
            add(Point(-2.695133221863356, 56.06702910083509))
            add(Point(-2.698223120515766, 56.06683745020233))
            add(Point(-2.701656341240667, 56.06645414607839))
            add(Point(-2.706119528183037, 56.06607083814320))
            add(Point(-2.710067732016672, 56.06559169786458))
            add(Point(-2.713329291705327, 56.06520838135397))
            add(Point(-2.716762512430227, 56.06453756828941))
            add(Point(-2.718307461756433, 56.06348340989081))
            add(Point(-2.719165766937657, 56.06281256681154))
            add(Point(-2.719852411082638, 56.06204587471371))
            add(Point(-2.719165766937657, 56.06166252294756))
            add(Point(-2.718307461756433, 56.06147084563517))
        }

        // create the polyline from the point collection
        return Polyline(boatPositions)
    }

    /**
     * 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 points = PointCollection(wgs84).apply {
            // add points to the point collection
            add(Point(-2.643077012566659, 56.07712534604447))
            add(Point(-2.642819521015944, 56.07717324600376))
            add(Point(-2.642540571836003, 56.07774804087097))
            add(Point(-2.642712232869812, 56.07792766250863))
            add(Point(-2.642454741319098, 56.07829887790651))
            add(Point(-2.641853927700763, 56.07852639525372))
            add(Point(-2.640974164902487, 56.07880180919243))
            add(Point(-2.639987113958079, 56.07881378366685))
            add(Point(-2.639407757968971, 56.07908919555142))
            add(Point(-2.638764029092183, 56.07917301616904))
            add(Point(-2.638485079912242, 56.07896945149566))
            add(Point(-2.638570910429147, 56.07820308072684))
            add(Point(-2.638785486721410, 56.07756841839600))
            add(Point(-2.639193181676709, 56.07719719596109))
            add(Point(-2.639944198699627, 56.07675411934114))
            add(Point(-2.640652300464093, 56.07673016910844))
            add(Point(-2.640673758093319, 56.07632301287509))
            add(Point(-2.640180232621116, 56.07599967986049))
            add(Point(-2.640244605508794, 56.07584400003405))
            add(Point(-2.640416266542604, 56.07578412301025))
            add(Point(-2.640888334385582, 56.07580807383093))
            add(Point(-2.641768097183858, 56.07623918605773))
            add(Point(-2.642197249768383, 56.07625116132851))
            add(Point(-2.642840978645171, 56.07661041772168))
            add(Point(-2.643077012566659, 56.07712534604447))
        }

        // create a polygon from the point collection
        return Polygon(points)
    }

    override fun onResume() {
        super.onResume()
        mapView.resume()
    }

    override fun onPause() {
        mapView.pause()
        super.onPause()
    }

    override fun onDestroy() {
        mapView.dispose()
        super.onDestroy()
    }
}

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