Add a point, line, and polygon
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 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.
To learn how to display data from data sources, see the Add a feature layer tutorial.
Prerequisites
The following are required for this tutorial:
- An ArcGIS account to access API keys. If you don't have an account, sign up for free.
- Confirm that your system meets the system requirements.
- An IDE for Android development in Kotlin.
Steps
Open an Android Studio project
To start this tutorial, complete the Display a map tutorial, or download and unzip the solution in a new folder.
Modify the old project for use in this new tutorial. Expand More info for instructions.
On your file system, delete the .idea folder, if present, at the top level of your project.
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.xmlChange line 1 2 3<resources> <string name="app_name">Add a point, line, and polygon</string> </resources>
In the Android tool window, open Gradle Scripts > settings.gradle.
Change the value of
rootProject.name
to "Add a point, line, and polygon".settings.gradleChange line 1 2include ':app' rootProject.name = "Add a point, line, and polygon"
Click File > Sync Project with Gradle files. Android Studio will recognize your changes and create a new .idea folder.
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.
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.
In Android Studio: in the Android tool window, open app > java > com.example.app > MainActivity.
In the
setupMap
function, set theapiKey
property on theArcGISRuntimeEnvironment
with your API key.MainActivity.ktChange line Change line Change line Change line 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 80 80/* * 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 * * https://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.example.app import android.os.Bundle import androidx.appcompat.app.AppCompatActivity import com.esri.arcgisruntime.ArcGISRuntimeEnvironment import com.esri.arcgisruntime.mapping.ArcGISMap import com.esri.arcgisruntime.mapping.BasemapStyle import com.esri.arcgisruntime.mapping.Viewpoint import com.esri.arcgisruntime.mapping.view.MapView import com.example.app.databinding.ActivityMainBinding class MainActivity : AppCompatActivity() { private val activityMainBinding by lazy { ActivityMainBinding.inflate(layoutInflater) } private val mapView: MapView by lazy { activityMainBinding.mapView } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(activityMainBinding.root) setupMap() } override fun onPause() { mapView.pause() super.onPause() } override fun onResume() { super.onResume() mapView.resume() } override fun onDestroy() { mapView.dispose() super.onDestroy() } // set up your map here. You will call this method from onCreate() private fun setupMap() { // 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. ArcGISRuntimeEnvironment.setApiKey("YOUR_API_KEY") // create a map with the BasemapStyle streets val map = ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC) // set the map to be displayed in the layout's MapView mapView.map = map // set the viewpoint, Viewpoint(latitude, longitude, scale) mapView.setViewpoint(Viewpoint(34.0270, -118.8050, 72000.0)) } }
Add import statements
Highlight the existing imports and replace with the imports needed for this tutorial.
MainActivity.ktChange line Change line Change line Change line Change line Change line Change line Change line Change line Change line Change line Change line Change line Change line Change line Change line Change line 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 38 37 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 36 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 -26 -27 -28 -29 -29 -29 -29 -29 -29 -29 -29 -29 -29 -29 -29 -29 -29 -29 -29 -29 -29 -29 -29/* * 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 * * https://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.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 class MainActivity : AppCompatActivity() { private val activityMainBinding by lazy { ActivityMainBinding.inflate(layoutInflater) } private val mapView: MapView by lazy { activityMainBinding.mapView } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(activityMainBinding.root) setupMap() addGraphics() } override fun onResume() { super.onResume() mapView.resume() } override fun onPause() { super.onPause() mapView.pause() } override fun onDestroy() { super.onDestroy() mapView.dispose() } 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 // 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) // 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) // 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) // 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) // 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) } // set up your map here. You will call this method from onCreate() private fun setupMap() { // 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. ArcGISRuntimeEnvironment.setApiKey("YOUR_API_KEY") // create a map with the basemap style topographic val map = ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC) // set the map to be displayed in the layout's MapView mapView.map = map // set the viewpoint, Viewpoint(latitude, longitude, scale) mapView.setViewpoint(Viewpoint(34.0270, -118.8050, 72000.0)) } }
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.
In Android Studio, in the Android tool window, open app > java > com.example.app > MainActivity.
Create a new function named
addGraphics
.Create a
GraphicsOverlay
to display point, line, and polygon graphics and add it to themapView
's collection of graphics overlays.Call the
addGraphics()
function from theonCreate
lifecycle function.MainActivity.ktAdd line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 51 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 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 82 82 82 82 82/* * 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 * * https://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.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 class MainActivity : AppCompatActivity() { private val activityMainBinding by lazy { ActivityMainBinding.inflate(layoutInflater) } private val mapView: MapView by lazy { activityMainBinding.mapView } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(activityMainBinding.root) setupMap() addGraphics() } override fun onResume() { super.onResume() mapView.resume() } override fun onPause() { super.onPause() mapView.pause() } override fun onDestroy() { super.onDestroy() mapView.dispose() } 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 // 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) // 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) // 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) // 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) // 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) } // set up your map here. You will call this method from onCreate() private fun setupMap() { // 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. ArcGISRuntimeEnvironment.setApiKey("YOUR_API_KEY") // create a map with the basemap style topographic val map = ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC) // set the map to be displayed in the layout's MapView mapView.map = map // set the viewpoint, Viewpoint(latitude, longitude, scale) mapView.setViewpoint(Viewpoint(34.0270, -118.8050, 72000.0)) } }
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
Point
and aSimpleMarkerSymbol
. To create thePoint
, provide longitude (x) and latitude (y) coordinates and aSpatialReference
. Use theSpatialReferences.getWgs84()
convenience function.Point graphics support a number of symbol types such as
SimpleMarkerSymbol
,PictureMarkerSymbol
, andTextSymbol
. Learn more about symbols in the API documentation.Next create a solid, blue, 2px-wide
SimpleLineSymbol
and assign it to theoutline
property ofsimpleMarkerSymbol
.MainActivity.ktAdd line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. 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 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 91 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92 92/* * 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 * * https://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.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 class MainActivity : AppCompatActivity() { private val activityMainBinding by lazy { ActivityMainBinding.inflate(layoutInflater) } private val mapView: MapView by lazy { activityMainBinding.mapView } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(activityMainBinding.root) setupMap() addGraphics() } override fun onResume() { super.onResume() mapView.resume() } override fun onPause() { super.onPause() mapView.pause() } override fun onDestroy() { super.onDestroy() mapView.dispose() } 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 // 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) // 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) // 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) // 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) // 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) } // set up your map here. You will call this method from onCreate() private fun setupMap() { // 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. ArcGISRuntimeEnvironment.setApiKey("YOUR_API_KEY") // create a map with the basemap style topographic val map = ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC) // set the map to be displayed in the layout's MapView mapView.map = map // set the viewpoint, Viewpoint(latitude, longitude, scale) mapView.setViewpoint(Viewpoint(34.0270, -118.8050, 72000.0)) } }
Create a
Graphic
with thepoint
andsimpleMarkerSymbol
. Display theGraphic
by adding it to thegraphicsOverlay
'sgraphics
collection.MainActivity.ktAdd line. Add line. Add line. Add line. Add line. 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 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 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55 55/* * 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 * * https://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.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 class MainActivity : AppCompatActivity() { private val activityMainBinding by lazy { ActivityMainBinding.inflate(layoutInflater) } private val mapView: MapView by lazy { activityMainBinding.mapView } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(activityMainBinding.root) setupMap() addGraphics() } override fun onResume() { super.onResume() mapView.resume() } override fun onPause() { super.onPause() mapView.pause() } override fun onDestroy() { super.onDestroy() mapView.dispose() } 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 // 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) // 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) // 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) // 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) // 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) } // set up your map here. You will call this method from onCreate() private fun setupMap() { // 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. ArcGISRuntimeEnvironment.setApiKey("YOUR_API_KEY") // create a map with the basemap style topographic val map = ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC) // set the map to be displayed in the layout's MapView mapView.map = map // set the viewpoint, Viewpoint(latitude, longitude, scale) mapView.setViewpoint(Viewpoint(34.0270, -118.8050, 72000.0)) } }
Click Run > Run > app to run the app.
An emulator should display, running your app.
If your app builds but no emulator displays, you need to add an emulator. Click Tools > AVD Manager > Create Virtual Device...
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
Polyline
and aSimpleLineSymbol
. To create thePolyline
, first create aPointCollection
and add individualPoint
s. Then pass thePointCollection
to thePolyline
constructor.Line graphics support a number of symbol types such as
SimpleMarkerSymbol
andTextSymbol
. Learn more about symbols in the API documentation.MainActivity.ktAdd 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. 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 111 111 111 111 111 110 109 108 107 106 105 104 103 102 101 100 99 98 97 96 95 94 93 92 91 90 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89 89/* * 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 * * https://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.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 class MainActivity : AppCompatActivity() { private val activityMainBinding by lazy { ActivityMainBinding.inflate(layoutInflater) } private val mapView: MapView by lazy { activityMainBinding.mapView } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(activityMainBinding.root) setupMap() addGraphics() } override fun onResume() { super.onResume() mapView.resume() } override fun onPause() { super.onPause() mapView.pause() } override fun onDestroy() { super.onDestroy() mapView.dispose() } 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 // 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) // 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) // 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) // 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) // 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) } // set up your map here. You will call this method from onCreate() private fun setupMap() { // 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. ArcGISRuntimeEnvironment.setApiKey("YOUR_API_KEY") // create a map with the basemap style topographic val map = ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC) // set the map to be displayed in the layout's MapView mapView.map = map // set the viewpoint, Viewpoint(latitude, longitude, scale) mapView.setViewpoint(Viewpoint(34.0270, -118.8050, 72000.0)) } }
Create a
Graphic
with thepolyline
andpolylineSymbol
. Display theGraphic
by adding it to thegraphicsOverlay
'sgraphics
collection. Next, add a blue outline.MainActivity.ktAdd line. Add line. Add line. Add line. Add line. 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 108 109 110 111 112 113 114 115 116 116 115 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100 99 98 97 96 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95 95/* * 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 * * https://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.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 class MainActivity : AppCompatActivity() { private val activityMainBinding by lazy { ActivityMainBinding.inflate(layoutInflater) } private val mapView: MapView by lazy { activityMainBinding.mapView } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(activityMainBinding.root) setupMap() addGraphics() } override fun onResume() { super.onResume() mapView.resume() } override fun onPause() { super.onPause() mapView.pause() } override fun onDestroy() { super.onDestroy() mapView.dispose() } 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 // 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) // 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) // 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) // 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) // 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) } // set up your map here. You will call this method from onCreate() private fun setupMap() { // 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. ArcGISRuntimeEnvironment.setApiKey("YOUR_API_KEY") // create a map with the basemap style topographic val map = ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC) // set the map to be displayed in the layout's MapView mapView.map = map // set the viewpoint, Viewpoint(latitude, longitude, scale) mapView.setViewpoint(Viewpoint(34.0270, -118.8050, 72000.0)) } }
Click Run > Run > app to run the app.
An emulator should display, running your app.
If your app builds but no emulator displays, you need to add an emulator. Click Tools > AVD Manager > Create Virtual Device...
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
Polygon
and aSimpleFillSymbol
. To create thePolygon
, first create aPointCollection
and add individualPoint
s. Then pass thePointCollection
to thePolygon
constructor.Polygon graphics support a number of symbol types such as
SimpleFillSymbol
,PictureFillSymbol
,SimpleMarkerSymbol
, andTextSymbol
. Learn more about symbols in the API documentation.Next, create a
SimpleFillSymbol
that has a solid, 20%-transparent orange fill, and theblueOutlineSymbol
defined earlier.MainActivity.ktAdd 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. 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 133 133 133 133 132 132 132 132 132 132 132 132 132 132 132 132 132 132 132 132 132 132 132 132 132 132/* * 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 * * https://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.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 class MainActivity : AppCompatActivity() { private val activityMainBinding by lazy { ActivityMainBinding.inflate(layoutInflater) } private val mapView: MapView by lazy { activityMainBinding.mapView } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(activityMainBinding.root) setupMap() addGraphics() } override fun onResume() { super.onResume() mapView.resume() } override fun onPause() { super.onPause() mapView.pause() } override fun onDestroy() { super.onDestroy() mapView.dispose() } 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 // 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) // 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) // 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) // 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) // 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) } // set up your map here. You will call this method from onCreate() private fun setupMap() { // 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. ArcGISRuntimeEnvironment.setApiKey("YOUR_API_KEY") // create a map with the basemap style topographic val map = ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC) // set the map to be displayed in the layout's MapView mapView.map = map // set the viewpoint, Viewpoint(latitude, longitude, scale) mapView.setViewpoint(Viewpoint(34.0270, -118.8050, 72000.0)) } }
Create a
Graphic
with thepolygon
andpolygonFillSymbol
. Display theGraphic
by adding it to thegraphicsOverlay
'sgraphics
collection.MainActivity.ktAdd line. Add line. Add line. Add line. 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 129 130 131 132 133 134 135 136 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137 137/* * 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 * * https://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.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 class MainActivity : AppCompatActivity() { private val activityMainBinding by lazy { ActivityMainBinding.inflate(layoutInflater) } private val mapView: MapView by lazy { activityMainBinding.mapView } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(activityMainBinding.root) setupMap() addGraphics() } override fun onResume() { super.onResume() mapView.resume() } override fun onPause() { super.onPause() mapView.pause() } override fun onDestroy() { super.onDestroy() mapView.dispose() } 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 // 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) // 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) // 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) // 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) // 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) } // set up your map here. You will call this method from onCreate() private fun setupMap() { // 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. ArcGISRuntimeEnvironment.setApiKey("YOUR_API_KEY") // create a map with the basemap style topographic val map = ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC) // set the map to be displayed in the layout's MapView mapView.map = map // set the viewpoint, Viewpoint(latitude, longitude, scale) mapView.setViewpoint(Viewpoint(34.0270, -118.8050, 72000.0)) } }
Click Run > Run > app to run the app.
An emulator should display, running your app.
If your app builds but no emulator displays, you need to add an emulator. Click Tools > AVD Manager > Create Virtual Device...
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 platform services, and ArcGIS tools in these tutorials: