Add a point, line, and polygon

Learn how to display point, line, and polygon in a .

add a point line and polygon

You typically use to display geographic data that is not connected to a database and that is not persisted, like highlighting a route between two locations, displaying a search buffer around a point, or tracking the location of a vehicle in real-time. Graphics are composed of a , , and .

In this tutorial, you display points, lines, and polygons on a map as .

To learn how to display data from , see the Add a feature layer tutorial.

Prerequisites

Before starting this tutorial:

  1. You need an ArcGIS Location Platform or ArcGIS Online account.

  2. Confirm that your system meets the system requirements.

  3. An IDE for Android development in Kotlin.

Steps

Open an Android Studio project

  1. To start this tutorial, complete the Display a map tutorial. Or download and unzip the Display a map solution in a new folder.

  2. Modify the old project for use in this new tutorial. Expand More info for instructions.

  3. If you downloaded the solution, get an access token and set the API key.

Add import statements

  1. Replace app-specific import statements with the imports needed for this tutorial.

    MainActivity.kt
    Expand
    17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
    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
    package com.example.app
    
    import android.os.Bundle
    import androidx.appcompat.app.AppCompatActivity
    
    import com.esri.arcgisruntime.ArcGISRuntimeEnvironment
    import com.esri.arcgisruntime.geometry.Point
    import com.esri.arcgisruntime.geometry.PointCollection
    import com.esri.arcgisruntime.geometry.Polygon
    import com.esri.arcgisruntime.geometry.Polyline
    import com.esri.arcgisruntime.geometry.SpatialReferences
    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.symbology.SimpleFillSymbol
    import com.esri.arcgisruntime.symbology.SimpleLineSymbol
    import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol
    
    import com.example.app.databinding.ActivityMainBinding
    
    Expand

Add a graphics overlay

A is a container for . It is used with a to display graphics on a . You can add more than one to a . Graphics overlays are displayed on top of all the other .

  1. In Android Studio, in the Android tool window, open app > java > com.example.app > MainActivity.

  2. Create a new function named addGraphics().

  3. Create a GraphicsOverlay to display point, line, and polygon graphics and add it to the mapView's collection of graphics overlays.

  4. Call the addGraphics() function from the onCreate() lifecycle function.

    MainActivity.kt
    Expand
    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
    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
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(activityMainBinding.root)
    
            setApiKeyForApp()
    
            setupMap()
    
            addGraphics()
    
        }
    
        override fun onResume() {
            super.onResume()
            mapView.resume()
        }
    
        override fun onPause() {
            super.onPause()
            mapView.pause()
        }
    
        override fun onDestroy() {
            mapView.dispose()
            super.onDestroy()
        }
    
        private fun setApiKeyForApp(){
    
            ArcGISRuntimeEnvironment.setApiKey("YOUR_ACCESS_TOKEN")
    
        }
    
        private fun addGraphics() {
    
            // create a graphics overlay and add it to the map view
            val graphicsOverlay = GraphicsOverlay()
            mapView.graphicsOverlays.add(graphicsOverlay)
    
        }
    
    Expand

Add a point graphic

A point is created using a and a . A point is defined with x and y coordinates, and a . For latitude and longitude coordinates, the spatial reference is WGS84.

  1. Create a Point and a SimpleMarkerSymbol. To create the Point, provide longitude (x) and latitude (y) coordinates and a SpatialReference. Use the SpatialReferences.getWgs84() convenience function.

    Next create a solid, blue, 2px-wide SimpleLineSymbol and assign it to the outline property of simpleMarkerSymbol.

    MainActivity.kt
    Expand
    84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    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
        private fun addGraphics() {
    
            // create a graphics overlay and add it to the map view
            val graphicsOverlay = GraphicsOverlay()
            mapView.graphicsOverlays.add(graphicsOverlay)
    
            // create a point geometry with a location and spatial reference
            // Point(latitude, longitude, spatial reference)
            val point = Point(-118.8065, 34.0005, SpatialReferences.getWgs84())
    
            // create an opaque orange (0xFFFF5733) point symbol with a blue (0xFF0063FF) outline symbol
            val simpleMarkerSymbol = SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CIRCLE, -0xa8cd, 10f)
    
            val blueOutlineSymbol = SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, -0xff9c01, 2f)
            simpleMarkerSymbol.outline = blueOutlineSymbol
    
        }
    
    Expand
  2. Create a Graphic with the point and simpleMarkerSymbol. Display the Graphic by adding it to the graphicsOverlay's graphics collection.

    MainActivity.kt
    Expand
    97 98 99 100 101 102 103 104
    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
            val blueOutlineSymbol = SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, -0xff9c01, 2f)
            simpleMarkerSymbol.outline = blueOutlineSymbol
    
            // create a graphic with the point geometry and symbol
            val pointGraphic = Graphic(point, simpleMarkerSymbol)
    
            // add the point graphic to the graphics overlay
            graphicsOverlay.graphics.add(pointGraphic)
    
    Expand
  3. Click Run > Run > app to run the app.

You should see a point graphic in Point Dume State Beach.

Add a line graphic

A line is created using a and a . A polyline is defined as a sequence of points.

  1. Create a Polyline and a SimpleLineSymbol. To create the Polyline, first create a PointCollection and add individual Points. Then pass the PointCollection to the Polyline constructor.

    MainActivity.kt
    Expand
    103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    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
            // add the point graphic to the graphics overlay
            graphicsOverlay.graphics.add(pointGraphic)
    
            // create a point collection with a spatial reference, and add three points to it
            val polylinePoints = PointCollection(SpatialReferences.getWgs84()).apply {
                // Point(latitude, longitude)
                add(Point(-118.8215, 34.0139))
                add(Point(-118.8148, 34.0080))
                add(Point(-118.8088, 34.0016))
            }
    
            // create a polyline geometry from the point collection
            val polyline = Polyline(polylinePoints)
    
            // create a blue line symbol for the polyline
            val polylineSymbol = SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, -0xff9c01, 3f)
    
    
    Expand
  2. Create a Graphic with the polyline and polylineSymbol. Display the Graphic by adding it to the graphicsOverlay's graphics collection. Next, add a blue outline.

    MainActivity.kt
    Expand
    117 118 119 120 121 122 123 124
    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
            // create a blue line symbol for the polyline
            val polylineSymbol = SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, -0xff9c01, 3f)
    
            // create a polyline graphic with the polyline geometry and symbol
            val polylineGraphic = Graphic(polyline, polylineSymbol)
    
            // add the polyline graphic to the graphics overlay
            graphicsOverlay.graphics.add(polylineGraphic)
    
    Expand
  3. Click Run > Run > app to run the app.

You should see a point and line graphic along Westward Beach.

Add a polygon graphic

A polygon is created using a and a . A polygon is defined as a sequence of points that describe a closed boundary.

  1. Create a Polygon and a SimpleFillSymbol. To create the Polygon, first create a PointCollection and add individual Points. Then pass the PointCollection to the Polygon constructor.

    Next, create a SimpleFillSymbol that has a solid, 20%-transparent orange fill, and the blueOutlineSymbol defined earlier.

    MainActivity.kt
    Expand
    123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    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
            // add the polyline graphic to the graphics overlay
            graphicsOverlay.graphics.add(polylineGraphic)
    
            // create a point collection with a spatial reference, and add five points to it
            val polygonPoints = PointCollection(SpatialReferences.getWgs84()).apply {
                // Point(latitude, longitude)
                add(Point(-118.8189, 34.0137))
                add(Point(-118.8067, 34.0215))
                add(Point(-118.7914, 34.0163))
                add(Point(-118.7959, 34.0085))
                add(Point(-118.8085, 34.0035))
            }
            // create a polygon geometry from the point collection
            val polygon = Polygon(polygonPoints)
    
            // create an orange fill symbol with 20% transparency and the blue simple line symbol
            val polygonFillSymbol =
                SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, -0x7f00a8cd, blueOutlineSymbol)
    
    
    Expand
  2. Create a Graphic with the polygon and polygonFillSymbol. Display the Graphic by adding it to the graphicsOverlay's graphics collection.

    MainActivity.kt
    Expand
    138 139 140 141 142 143 144 145
    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
            // create an orange fill symbol with 20% transparency and the blue simple line symbol
            val polygonFillSymbol =
                SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, -0x7f00a8cd, blueOutlineSymbol)
    
            // create a polygon graphic from the polygon geometry and symbol
            val polygonGraphic = Graphic(polygon, polygonFillSymbol)
            // add the polygon graphic to the graphics overlay
            graphicsOverlay.graphics.add(polygonGraphic)
    
    Expand
  3. Click Run > Run > app to run the app.

You should see a point, line, and polygon graphic around Mahou Riviera in the Santa Monica Mountains.

What's next

Learn how to use additional API features, ArcGIS location services, and ArcGIS tools in these tutorials:

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

You can no longer sign into this site. Go to your ArcGIS portal or the ArcGIS Location Platform dashboard to perform management tasks.

Your ArcGIS portal

Create, manage, and access API keys and OAuth 2.0 developer credentials, hosted layers, and data services.

Your ArcGIS Location Platform dashboard

Manage billing, monitor service usage, and access additional resources.

Learn more about these changes in the What's new in Esri Developers June 2024 blog post.

Close