Add a feature layer

Learn how to use a URL to access and display a feature layer in a map.

add a feature layer

A feature layer is a visual representation of feature table data. Feature layers contain features with a geometry type (point, line, or polygon), and attributes. You can use feature layers to store, access, and manage large amounts of geographic data for your applications. You can access features from a feature layer using its underlying feature table.

In this tutorial, you use a URL to create, access, and display a feature layer in your app by first creating a local feature table from an ArcGIS Online service: Trailheads (points).

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 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 feature layer.

      strings.xml
      1 2 3
      Change line
      1
      2
      3
      <resources>
          <string name="app_name">Add a feature layer</string>
      </resources>
    3. In the Android tool window, open Gradle Scripts > settings.gradle.

      Change the value of rootProject.name to "Add a feature layer".

      settings.gradle
      1 2
      Change line
      1
      2
      include ':app'
      rootProject.name = "Add a feature layer"
    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 setupMap function, set the apiKey property on the ArcGISRuntimeEnvironment with your API key.

      MainActivity.kt
      64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 81 81
      Change 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
      /*
       * 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

  1. Add import statements to reference the additional ArcGIS Runtime API classes needed for this tutorial.

    MainActivity.kt
    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 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32
    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
    /*
     * 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 androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    
    import com.esri.arcgisruntime.ArcGISRuntimeEnvironment
    
    import com.esri.arcgisruntime.data.ServiceFeatureTable
    import com.esri.arcgisruntime.layers.FeatureLayer
    
    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 topographic
            val map = ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC)
    
            // create the service feature table
            val serviceFeatureTable =
                ServiceFeatureTable("https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads/FeatureServer/0")
    
            // create the feature layer using the service feature table
            val featureLayer = FeatureLayer(serviceFeatureTable)
            map.operationalLayers.add(featureLayer)
    
            // 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, 200000.0))
    
        }
    
    }
    

Change the view point scale

  1. In the setupMap() method, modify the Viewpoint constructor call so it passes a scale parameter more appropriate to this tutorial.

    MainActivity.kt
    68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 68 69 70 71 72 73 74 75 76 77 78 79 79 79 79 79 79 79 79 79 80 81 82 83 84 85 86 86 86
    Change 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
    /*
     * 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 androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    
    import com.esri.arcgisruntime.ArcGISRuntimeEnvironment
    
    import com.esri.arcgisruntime.data.ServiceFeatureTable
    import com.esri.arcgisruntime.layers.FeatureLayer
    
    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 topographic
            val map = ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC)
    
            // create the service feature table
            val serviceFeatureTable =
                ServiceFeatureTable("https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads/FeatureServer/0")
    
            // create the feature layer using the service feature table
            val featureLayer = FeatureLayer(serviceFeatureTable)
            map.operationalLayers.add(featureLayer)
    
            // 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, 200000.0))
    
        }
    
    }
    

Create a service feature table to reference feature service data

To display a new data layer (also known as an operational layer) on top of the current basemap, you will create a ServiceFeatureTable using an URL to reference the dataset hosted in ArcGIS Online.

  1. Open a browser and navigate to the URL for Parks and Open Spaces to view metadata about the layer. To display the layer in your ArcGIS Runtime app, you only need the URL.

    The service page provides information such as the geometry type, the geographic extent, the minimum and maximum scale at which features are visible, and the attributes (fields) it contains. You can preview the layer by clicking on ArcGIS.com Map in the "View In:" list at the top of the page.

  2. In the setupMap() method, create a ServiceFeatureTable, using a string URL to reference the dataset. You will add Trailheads (points).

    A ServiceFeatureTable is effectively an in-memory database of the features from the service.

    MainActivity.kt
    71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 71 72 73 74 75 76 77 78 79 80 81 82 82 81 80 79 78 78 78 78 78 78 78 78 78 78
    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
    /*
     * 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 androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    
    import com.esri.arcgisruntime.ArcGISRuntimeEnvironment
    
    import com.esri.arcgisruntime.data.ServiceFeatureTable
    import com.esri.arcgisruntime.layers.FeatureLayer
    
    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 topographic
            val map = ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC)
    
            // create the service feature table
            val serviceFeatureTable =
                ServiceFeatureTable("https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads/FeatureServer/0")
    
            // create the feature layer using the service feature table
            val featureLayer = FeatureLayer(serviceFeatureTable)
            map.operationalLayers.add(featureLayer)
    
            // 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, 200000.0))
    
        }
    
    }
    

Create a feature layer to display the hosted data

You will create a new FeatureLayer to display the hosted layer above the basemap.

  1. Create a new FeatureLayer using the service feature table and add it to the map as a data (operational) layer.

    If an app has multiple data layers, they are displayed in the order in which they are added to the app.

    A FeatureLayer is simply a reference to a feature service and a fast and easy way to add data to a map. It is accessed via a URL which specifies the endpoint. By default, the API will try to load all of the features that fit into the current view.

    MainActivity.kt
    79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 79 80 81 82 83 84 85 86 86 86 86 86 86 86 86 86 86 86
    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
    /*
     * 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 androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    
    import com.esri.arcgisruntime.ArcGISRuntimeEnvironment
    
    import com.esri.arcgisruntime.data.ServiceFeatureTable
    import com.esri.arcgisruntime.layers.FeatureLayer
    
    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 topographic
            val map = ArcGISMap(BasemapStyle.ARCGIS_TOPOGRAPHIC)
    
            // create the service feature table
            val serviceFeatureTable =
                ServiceFeatureTable("https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads/FeatureServer/0")
    
            // create the feature layer using the service feature table
            val featureLayer = FeatureLayer(serviceFeatureTable)
            map.operationalLayers.add(featureLayer)
    
            // 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, 200000.0))
    
        }
    
    }
    
  2. Click Run > Run > app to run the app.

You should see point features (representing trailheads) draw on the map for an area in the Santa Monica Mountains in southern California.

What's next?

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

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