Add a point, line, and polygon

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

add a point line and polygon

You typically use graphics 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 geometry, symbol, and attributes.

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

Prerequisites

The following are required for this tutorial:

  1. An ArcGIS account to access API keys. If you don't have an account, sign up for free.
  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.

    1. On your file system, delete the .idea folder, if present, at the top level of your project.

    2. In the Android tool window, open app > res > values > strings.xml.

      In the <string name="app_name"> element, change the text content to Add a point, line, and polygon.

      strings.xml
      Use dark colors for code blocks
      1 2 3 4 5
      Change line
      1
      2
      3
      4
      5
      <resources>
      
          <string name="app_name">Add a point, line, and polygon</string>
      
      </resources>
    3. In the Android tool window, open Gradle Scripts > settings.gradle.

      Change the value of rootProject.name to "Add a point, line, and polygon".

      settings.gradle
      Expand
      Use dark colors for code blocks
      19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 20 21 22
      Change line
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      rootProject.name = "Add a point, line, and polygon"
      
      include ':app'
    4. Click File > Sync Project with Gradle files. Android Studio will recognize your changes and create a new .idea folder.

  3. If you downloaded the solution project, set your API key.

    An API Key enables access to services, web maps, and web scenes hosted in ArcGIS Online.

    1. Go to your developer dashboard to get your API key. For these tutorials, use your default API key. It is scoped to include all of the services demonstrated in the tutorials.

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

    3. In the setApiKey() method, find the ApiKey.create() call and paste your API key inside the quotes, replacing YOUR_API_KEY.

      MainActivity.kt
      Use dark colors for code blocks
                                                                               
      Change line
      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
          private fun setApiKey() {
      
              // It is not best practice to store API keys in source code. We have you insert one here
              // to streamline this tutorial.
      
              ArcGISEnvironment.apiKey = ApiKey.create("YOUR_API_KEY")
      
          }
      

Add import statements

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

    MainActivity.kt
    Expand
    Use dark colors for code blocks
    17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 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 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 45 44 43 43 43 43 43 43 43 43 43 43 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 -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 -25 -25 -25 -25 -25 -25 -25 -25 -25 -25 -25 -25 -25 -25
    Change lineChange lineChange lineChange lineChange lineChange lineChange lineChange lineChange lineChange lineChange lineChange lineChange lineChange lineChange lineChange lineChange lineChange lineChange lineChange lineChange lineChange lineChange lineChange line
    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
    package com.example.app
    
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    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.PolygonBuilder
    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.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.view.Graphic
    import com.arcgismaps.mapping.view.GraphicsOverlay
    import com.arcgismaps.mapping.view.MapView
    import com.example.app.databinding.ActivityMainBinding
    
    
    class MainActivity : AppCompatActivity() {
    
    Expand

Add a graphics overlay

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

  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
    Use dark colors for code blocks
    54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 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 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 81 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82 82
    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.
    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
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
    
            lifecycle.addObserver(mapView)
    
            setApiKey()
    
            setupMap()
    
            addGraphics()
    
        }
    
        private fun setApiKey() {
            // set your API key
            // Note: it is not best practice to store API keys in source code. The API key is referenced
            // here for the convenience of this tutorial.
            ArcGISEnvironment.apiKey = ApiKey.create("YOUR_API_KEY")
    
        }
    
        private fun addGraphics() {
    
            // create a graphics overlay and add it to the graphicsOverlays property of the map view
            val graphicsOverlay = GraphicsOverlay()
            mapView.graphicsOverlays.add(graphicsOverlay)
    
        }
    
    Expand

Add a point graphic

A point graphic is created using a point and a marker symbol. A point is defined with x and y coordinates, and a spatial reference. 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. In this tutorial, call wgs84() on SpatialReference.

    Point graphics support a number of symbol types such as SimpleMarkerSymbol, PictureMarkerSymbol, and TextSymbol. Learn more about symbols at Symbol in the API Reference.

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

    MainActivity.kt
    Expand
    Use dark colors for code blocks
    75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 93 93 93 93 93 93 93 93 93 93 93 93 93 93 93 93
    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.
    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
        private fun addGraphics() {
    
            // create a graphics overlay and add it to the graphicsOverlays property of 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, SpatialReference.wgs84())
    
            // create a point symbol that is an small red circle
            val simpleMarkerSymbol = SimpleMarkerSymbol(SimpleMarkerSymbolStyle.Circle, Color.red, 10f)
    
            // create a blue outline symbol and assign it to the outline property of the simple marker symbol
            val blueOutlineSymbol = SimpleLineSymbol(SimpleLineSymbolStyle.Solid, Color.fromRgba(0, 0, 255), 2f)
            simpleMarkerSymbol.outline = blueOutlineSymbol
    
        }
    
    Expand
  2. Create a Graphic with the point and simpleMarkerSymbol. Display the graphic by adding it to the graphics property of graphicsOverlay.

    MainActivity.kt
    Expand
    Use dark colors for code blocks
    88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 88 89 90 91 92 93 94 95 96 97 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54 54
    Add line.Add line.Add line.Add line.Add line.
    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
            // create a blue outline symbol and assign it to the outline property of the simple marker symbol
            val blueOutlineSymbol = SimpleLineSymbol(SimpleLineSymbolStyle.Solid, Color.fromRgba(0, 0, 255), 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.

    In Android Studio, you have two choices for running your app: an actual Android device or the Android Emulator.

    Android device

    Connect your computer to your Android device, using USB or Wi-Fi. For more details, see How to connect your Android device.

    Android Emulator

    Create an AVD (Android Virtual Device) to run in the Android Emulator. For details, see Run apps on the Android Emulator.

    Selecting a device

    When you build and run an app in Android Studio, you must first select a device. From the Android Studio toolbar, you can access the drop-down list of your currently available devices, both virtual and physical.

    Drop-down for available devcies in Android Studio .

    If you cannot access the list on the toolbar, click Tools > Device Manager.

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

Add a line graphic

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

Polylines have one or more distinct parts. Each part is a sequence of points. For a continuous line, you can use the Polyline constructor to create a polyline with just one part. To create a polyline with more than one part, use a PolylineBuilder.

  1. Create a Polyline and a SimpleLineSymbol. To create the polyline: create a PolylineBuilder, and then call toGeometry() on the polygon builder to get the polyline.

    Line graphics support a number of symbol types such as SimpleMarkerSymbol and TextSymbol. Learn more about symbols at Symbol in the API Reference.

    MainActivity.kt
    Expand
    Use dark colors for code blocks
    95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 110 110 110 110 110 109 108 107 106 105 104 103 102 101 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84 84
    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.
    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
            // add the point graphic to the graphics overlay
            graphicsOverlay.graphics.add(pointGraphic)
    
            // Create a polylineBuilder with a spatial reference and add three points to it.
            // Then get the polyline from the polyline builder
    
            // create a blue line symbol for the polyline
            val polylineSymbol = SimpleLineSymbol(SimpleLineSymbolStyle.Solid, Color.fromRgba(0, 0, 255), 3f)
            val polylineBuilder = PolylineBuilder(SpatialReference.wgs84()) {
                addPoint(-118.8215, 34.0139)
                addPoint(-118.8148, 34.0080)
                addPoint(-118.8088, 34.0016)
            }
            val polyline = polylineBuilder.toGeometry()
    
    
    Expand
  2. Create a Graphic with the polyline and polylineSymbol. Display the graphic by adding it to the graphics property of graphicsOverlay.

    MainActivity.kt
    Expand
    Use dark colors for code blocks
    101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 115 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100 99 98 97 96 95 94 93 92 91 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90
    Add line.Add line.Add line.Add line.Add line.
    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
            // create a blue line symbol for the polyline
            val polylineSymbol = SimpleLineSymbol(SimpleLineSymbolStyle.Solid, Color.fromRgba(0, 0, 255), 3f)
            val polylineBuilder = PolylineBuilder(SpatialReference.wgs84()) {
                addPoint(-118.8215, 34.0139)
                addPoint(-118.8148, 34.0080)
                addPoint(-118.8088, 34.0016)
            }
            val polyline = polylineBuilder.toGeometry()
    
            // 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.

    In Android Studio, you have two choices for running your app: an actual Android device or the Android Emulator.

    Android device

    Connect your computer to your Android device, using USB or Wi-Fi. For more details, see How to connect your Android device.

    Android Emulator

    Create an AVD (Android Virtual Device) to run in the Android Emulator. For details, see Run apps on the Android Emulator.

    Selecting a device

    When you build and run an app in Android Studio, you must first select a device. From the Android Studio toolbar, you can access the drop-down list of your currently available devices, both virtual and physical.

    Drop-down for available devcies in Android Studio .

    If you cannot access the list on the toolbar, click Tools > Device Manager.

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

Add a polygon graphic

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

Polygons have one or more distinct parts. Each part is a sequence of points describing a closed boundary. For a single area with no holes, you can use the Polygon constructor to create a polygon with just one part. To create a polygon with more than one part, use a PolygonBuilder.

  1. Create a Polygon and a SimpleFillSymbol. To create the polygon: create a PolygonBuilder, and then call toGeometry() on the polygon builder to get the polygon.

    Polygon graphics support a number of symbol types such as SimpleFillSymbol, PictureFillSymbol, SimpleMarkerSymbol and TextSymbol. Learn more about symbols at Symbol in the API Reference.

    Next, create a SimpleFillSymbol that has a solid red fill with an alpha channel of 128 and uses the blueOutlineSymbol defined earlier.

    MainActivity.kt
    Expand
    Use dark colors for code blocks
    113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 136 136 136 136 135 135 135 135 135 135 135 135 135 135 135 135 135 135 135 135 135
    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.
    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
            // add the polyline graphic to the graphics overlay
            graphicsOverlay.graphics.add(polylineGraphic)
    
            // Create a polygon builder with a spatial reference and add five vertices (points) to it.
            // Then get the polygon from the polygon builder.
            val polygonBuilder = PolygonBuilder(SpatialReference.wgs84()) {
                addPoint(-118.8189, 34.0137)
                addPoint(-118.8067, 34.0215)
                addPoint(-118.7914, 34.0163)
                addPoint(-118.7959, 34.0085)
                addPoint(-118.8085, 34.0035)
            }
            val polygon = polygonBuilder.toGeometry()
    
            // Create a red fill symbol with an alpha component of 128: values can run from 0 to 255).
            // Then crete the blue simple line symbol
            val polygonFillSymbol =
                SimpleFillSymbol(
                    SimpleFillSymbolStyle.Solid,
                    Color.fromRgba(255, 0, 0, 128),
                    blueOutlineSymbol
                )
    
    
    Expand
  2. Create a Graphic with the polygon and polygonFillSymbol. Display the graphic by adding it to the graphics property of graphicsOverlay.

    MainActivity.kt
    Expand
    Use dark colors for code blocks
    127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 127 128 129 130 131 132 133 134 135 136 137 138 139 140 140 140 140 140 140 140 140 140 140 140 140 140 140 140 140 140 140
    Add line.Add line.Add line.Add line.
    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
            // Create a red fill symbol with an alpha component of 128: values can run from 0 to 255).
            // Then crete the blue simple line symbol
            val polygonFillSymbol =
                SimpleFillSymbol(
                    SimpleFillSymbolStyle.Solid,
                    Color.fromRgba(255, 0, 0, 128),
                    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.

    In Android Studio, you have two choices for running your app: an actual Android device or the Android Emulator.

    Android device

    Connect your computer to your Android device, using USB or Wi-Fi. For more details, see How to connect your Android device.

    Android Emulator

    Create an AVD (Android Virtual Device) to run in the Android Emulator. For details, see Run apps on the Android Emulator.

    Selecting a device

    When you build and run an app in Android Studio, you must first select a device. From the Android Studio toolbar, you can access the drop-down list of your currently available devices, both virtual and physical.

    Drop-down for available devcies in Android Studio .

    If you cannot access the list on the toolbar, click Tools > Device Manager.

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

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