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

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 selected point, or showing the location of map coordinates entered by the user. Graphics are composed of a geometry, symbol, and attributes.
In this tutorial, you display points, lines, and polygons on a map as graphics.
Prerequisites
Before starting this tutorial, you need the following:
-
An ArcGIS Location Platform or ArcGIS Online account.
-
A development and deployment environment that meets the system requirements.
-
An IDE for Android development in Kotlin.
Develop or download
You have two options for completing this tutorial:
Option 1: Develop the code
Open an Android Studio project
-
Open the project you created by completing the Display a map tutorial.
-
Continue with the following instructions to display a point, line, and polygon in the map.
-
Modify the old project for use in this new tutorial.
-
On your file system, delete the .idea folder, if present, at the top level of your project.
-
In the Android view, open app > res > values > strings.xml.
In the
<string name="app
element, change the text content to Add a point, line, and polygon._name" > strings.xmlUse dark colors for code blocks <resources> <string name="app_name">Add a point, line, and polygon</string> </resources>
-
In the Android view, open Gradle Scripts > settings.gradle.kts.
Change the value of
root
to "Add a point, line, and polygon".Project.name settings.gradle.ktsUse dark colors for code blocks dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() maven { url = uri("https://esri.jfrog.io/artifactory/arcgis") } maven { url = uri("https://olympus.esri.com/artifactory/arcgisruntime-repo/") } } } rootProject.name = "Add a point, line, and polygon" include(":app")
-
Click File > Sync Project with Gradle files. Android Studio will recognize your changes and create a new .idea folder.
-
Add import statements
-
In the Android view, open app > kotlin+java > com.example.app > screens > MainScreen.kt. Replace the import statements with the imports needed for this tutorial.
MainScreen.ktUse dark colors for code blocks @file:OptIn(ExperimentalMaterial3Api::class) package com.example.app.screens import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.padding import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.Scaffold import androidx.compose.material3.Text import androidx.compose.material3.TopAppBar import androidx.compose.runtime.Composable import androidx.compose.runtime.remember import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource 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.toolkit.geoviewcompose.MapView import com.example.app.R // Create a blue outline symbol.
Add a graphics overlay and a list of graphics overlay
You will create a GraphicsOverlay
. Then add the graphics overlay to a list named graphics
.
For information about remembering state between recompositions in Android Jetpack Compose, see State and Jetpack Compose.
In subsequent sections of this tutorial, you will create a point, a line, and a polygon and add each of them to the GraphicsOverlay
.
-
In the
Main
composable, add aScreen remember
block and create aGraphicsOverlay
inside it. Then assignremember
to a local variable namedgraphics
.Overlay MainScreen.ktUse dark colors for code blocks @Composable fun MainScreen() { // create an ArcGISMap val map = remember { createMap() } // Create a graphics overlay. val graphicsOverlay = remember { GraphicsOverlay() } Scaffold( topBar = { TopAppBar(title = { Text(text = stringResource(id = R.string.app_name)) }) } ) { MapView( modifier = Modifier.fillMaxSize().padding(it), arcGISMap = map, ) }
-
Add a
remember
block. Inside it, create a list that contains thegraphics
you just created. Then assignOverlay remember
to a local variable namedgraphics
.Overlays MainScreen.ktUse dark colors for code blocks @Composable fun MainScreen() { // create an ArcGISMap val map = remember { createMap() } // Create a graphics overlay. val graphicsOverlay = remember { GraphicsOverlay() } // Create a list of graphics overlays used by the MapView val graphicsOverlays = remember { listOf(graphicsOverlay) }
-
Pass
graphics
as theOverlays graphics
argument toOverlays MapView
.MainScreen.ktUse dark colors for code blocks Scaffold( topBar = { TopAppBar(title = { Text(text = stringResource(id = R.string.app_name)) }) } ) { MapView( modifier = Modifier.fillMaxSize().padding(it), arcGISMap = map, graphicsOverlays = graphicsOverlays ) }
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.
-
Create a lazy top-level property named
blue
. In theOutline Symbol lazy
block, create a solid, blue, 2px-wideSimpleLineSymbol
.MainScreen.ktUse dark colors for code blocks // Create a blue outline symbol. private val blueOutlineSymbol by lazy { SimpleLineSymbol(SimpleLineSymbolStyle.Solid, Color.fromRgba(0, 0, 255), 2f) }
-
Create a lazy top-level property named
point
.Graphic MainScreen.ktUse dark colors for code blocks private val pointGraphic by lazy { }
-
In the
lazy
block of thepoint
property , create aGraphic Point
and aSimpleMarkerSymbol
. To create the point, provide x (longitude) and y (latitude) coordinates and aSpatialReference
(by callingwgs84()
onSpatialReference
).Point graphics support a number of symbol types such as
SimpleMarkerSymbol
,PictureMarkerSymbol
, andTextSymbol
. Learn more about symbols atSymbol
in the API Reference.Assign
blue
to theOutline Symbol outline
property ofsimple
.Marker Symbol MainScreen.ktUse dark colors for code blocks private val pointGraphic by lazy { // Create a point geometry with a location and spatial reference. // Point(latitude, longitude, spatial reference) val point = Point( x = -118.8065, y = 34.0005, spatialReference = SpatialReference.wgs84() ) // Create a point symbol that is an small red circle and assign the blue outline symbol to its outline property. val simpleMarkerSymbol = SimpleMarkerSymbol( style = SimpleMarkerSymbolStyle.Circle, color = Color.red, size = 10f ) simpleMarkerSymbol.outline = blueOutlineSymbol }
-
Create a
Graphic
withpoint
andsimple
.Marker Symbol MainScreen.ktUse dark colors for code blocks private val pointGraphic by lazy { // Create a point geometry with a location and spatial reference. // Point(latitude, longitude, spatial reference) val point = Point( x = -118.8065, y = 34.0005, spatialReference = SpatialReference.wgs84() ) // Create a point symbol that is an small red circle and assign the blue outline symbol to its outline property. val simpleMarkerSymbol = SimpleMarkerSymbol( style = SimpleMarkerSymbolStyle.Circle, color = Color.red, size = 10f ) simpleMarkerSymbol.outline = blueOutlineSymbol // Create a graphic with the point geometry and symbol. Graphic( geometry = point, symbol = simpleMarkerSymbol ) }
-
In
Main
, display the graphic by adding it to theScreen graphics
property ofgraphics
.Overlay MainScreen.ktUse dark colors for code blocks @Composable fun MainScreen() { // create an ArcGISMap val map = remember { createMap() } // Create a graphics overlay. val graphicsOverlay = remember { GraphicsOverlay() } // Add the point graphic to the graphics overlay. graphicsOverlay.graphics.add(pointGraphic) // Create a list of graphics overlays used by the MapView val graphicsOverlays = remember { listOf(graphicsOverlay) }
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.
.
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
.
-
Create a lazy top-level property named
polyline
.Graphic MainScreen.ktUse dark colors for code blocks private val polylineGraphic by lazy { }
-
In the
lazy
block, create aPolyline
and aSimpleLineSymbol
. To create the polyline, create aPolylineBuilder
and then callto
on the polygon builder.Geometry() Line graphics support a number of symbol types such as
SimpleMarkerSymbol
andTextSymbol
. Learn more about symbols atSymbol
in the API Reference.MainScreen.ktUse dark colors for code blocks private val polylineGraphic by lazy { // Create a blue line symbol for the polyline. val polylineSymbol = SimpleLineSymbol( style = SimpleLineSymbolStyle.Solid, color = Color.fromRgba(0, 0, 255), width = 3f ) // Create a polylineBuilder with a spatial reference and add three points to it. val polylineBuilder = PolylineBuilder(SpatialReference.wgs84()) { addPoint(-118.8215, 34.0139) addPoint(-118.8148, 34.0080) addPoint(-118.8088, 34.0016) } // then get the polyline from the polyline builder val polyline = polylineBuilder.toGeometry() }
-
Create a
Graphic
withpolyline
andpolyline
.Symbol MainScreen.ktUse dark colors for code blocks private val polylineGraphic by lazy { // Create a blue line symbol for the polyline. val polylineSymbol = SimpleLineSymbol( style = SimpleLineSymbolStyle.Solid, color = Color.fromRgba(0, 0, 255), width = 3f ) // Create a polylineBuilder with a spatial reference and add three points to it. val polylineBuilder = PolylineBuilder(SpatialReference.wgs84()) { addPoint(-118.8215, 34.0139) addPoint(-118.8148, 34.0080) addPoint(-118.8088, 34.0016) } // then get the polyline from the polyline builder val polyline = polylineBuilder.toGeometry() // Create a polyline graphic with the polyline geometry and symbol. Graphic( geometry = polyline, symbol = polylineSymbol ) }
-
In
Main
, display the graphic by adding it to theScreen graphics
property ofgraphics
.Overlay MainScreen.ktUse dark colors for code blocks @Composable fun MainScreen() { // create an ArcGISMap val map = remember { createMap() } // Create a graphics overlay. val graphicsOverlay = remember { GraphicsOverlay() } // Add the point graphic to the graphics overlay. graphicsOverlay.graphics.add(pointGraphic) // Add the polyline graphic to the graphics overlay. graphicsOverlay.graphics.add(polylineGraphic) // Create a list of graphics overlays used by the MapView val graphicsOverlays = remember { listOf(graphicsOverlay) }
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.
.
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
.
-
Create a lazy top-level property named
polygon
.Graphic MainScreen.ktUse dark colors for code blocks private val polygonGraphic by lazy { }
-
In the
lazy
block, create aPolygon
and aSimpleFillSymbol
. To create the polygon, create aPolygonBuilder
and then callto
on the polygon builder.Geometry() Polygon graphics support a number of symbol types such as
SimpleFillSymbol
,PictureFillSymbol
,SimpleMarkerSymbol
andTextSymbol
. Learn more about symbols atSymbol
in the API Reference.Next, create a
SimpleFillSymbol
that has a solid red fill with an alpha channel of 128 and uses theblue
defined earlier.Outline Symbol MainScreen.ktUse dark colors for code blocks private val polygonGraphic by lazy { // 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). val polygonFillSymbol = SimpleFillSymbol( style = SimpleFillSymbolStyle.Solid, color = Color.fromRgba(255, 0, 0, 128), outline = blueOutlineSymbol ) }
-
Create a
Graphic
withpolygon
andpolygon
.Fill Symbol MainScreen.ktUse dark colors for code blocks private val polygonGraphic by lazy { // 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). val polygonFillSymbol = SimpleFillSymbol( style = SimpleFillSymbolStyle.Solid, color = Color.fromRgba(255, 0, 0, 128), outline = blueOutlineSymbol ) // Create a polygon graphic from the polygon geometry and symbol. Graphic( geometry = polygon, symbol = polygonFillSymbol ) }
-
In
Main
, display the graphic by adding it to theScreen graphics
property ofgraphics
.Overlay MainScreen.ktUse dark colors for code blocks @Composable fun MainScreen() { // create an ArcGISMap val map = remember { createMap() } // Create a graphics overlay. val graphicsOverlay = remember { GraphicsOverlay() } // Add the point graphic to the graphics overlay. graphicsOverlay.graphics.add(pointGraphic) // Add the polyline graphic to the graphics overlay. graphicsOverlay.graphics.add(polylineGraphic) // Add the polygon graphic to the graphics overlay. graphicsOverlay.graphics.add(polygonGraphic) // Create a list of graphics overlays used by the MapView val graphicsOverlays = remember { listOf(graphicsOverlay) }
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.
.
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.
Alternatively, you can download the tutorial solution, as follows.
Option 2: Download the solution
-
Click the Download solution link in the right-hand side of this page.
-
Unzip the file to a location on your machine.
-
Run Android Studio.
-
Go to File > Open.... Navigate to the solution folder and click Open.
On Windows: If you are in the Welcome to Android Studio dialog, click Open and navigate to the solution folder. Then click Open.
Since the downloaded solution does not contain authentication credentials, you must first set up authentication to create credentials, and then add the developer credentials to the solution.
Set up authentication
To access the secure ArcGIS location services used in this tutorial, you must implement API key authentication or user authentication using an ArcGIS Location Platform or an ArcGIS Online account.
You can implement API key authentication or user authentication in this tutorial. Compare the differences below:
API key authentication
- Users are not required to sign in.
- Requires creating an API key credential with the correct privileges.
- API keys are long-lived access tokens.
- Service usage is billed to the API key owner/developer.
- Simplest authentication method to implement.
- Recommended approach for new ArcGIS developers.
Learn more in API key authentication.
User authentication
- Users are required to sign in with an ArcGIS account.
- User accounts must have privilege to access the ArcGIS services used in application.
- Requires creating OAuth credentials.
- Application uses a redirect URL and client ID.
- Service usage is billed to the organization of the user signed into the application.
Learn more in User authentication.
Create a new API key access token with privileges to access the secure resources used in this tutorial.
-
Complete the Create an API key tutorial and create an API key with the following privilege(s):
- Privileges
- Location services > Basemaps
- Privileges
-
Copy and paste the API key access token into a safe location. It will be used in a later step.
Set developer credentials in the solution
To allow your app users to access ArcGIS location services, use the developer credentials that you created in the Set up authentication step to authenticate requests for resources.
-
In the Android view of Android Studio, open app > kotlin+java > com.example.app > MainActivity. Set the
Authentication
toMode .
.API _KEY MainActivity.ktUse dark colors for code blocks class MainActivity : ComponentActivity() { private enum class AuthenticationMode { API_KEY, USER_AUTH } private val authenticationMode = AuthenticationMode.API_KEY
-
Set the
api
property with your API key access token.Key MainActivity.ktUse dark colors for code blocks override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) when (authenticationMode) { AuthenticationMode.API_KEY -> { ArcGISEnvironment.apiKey = ApiKey.create("YOUR_ACCESS_TOKEN") }
Best Practice: The access token is stored directly in the code as a convenience for this tutorial. Do not store credentials directly in source code in a production environment.
Run the app
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: