Overview

You will learn: how to load a layer from an ArcGIS Online item and add it to a map.

The ArcGIS Runtime SDK for .NET allows you to access items (such as layers) stored in ArcGIS Online by their item identifier. Feature layers can store geographic features (points, lines, polygons), edit or update attributes, and synchronize changes. As demonstrated in the lab Display a web map, you can load and display an existing web map that was preconfigured with a basemap and layers. Alternatively, you may wish to load one or more layers from ArcGIS Online and create a map with code. You may do this if you have operational or base layers from different sources in your app. You can publish your own feature layer to ArcGIS Online using the Create a new dataset data lab.

In this lab you will learn how to access a layer stored in ArcGIS Online. You will open the layer using its identifier and display its contents on a map.

Steps

Create the Project

  1. Start Visual Studio.

  2. Choose File > New > Project and select the ArcGIS Runtime App template for your chosen platform.

Create the portal

  1. Open the MapViewModel.cs code file in your project.

  2. Add the following function to the MapViewModel class:

    private async void InitializeMap()
    {
    
    }
    
  3. Add code to the initialization function to create the portal:

    ArcGISPortal portal = await ArcGISPortal.CreateAsync();
    

Load the portal item

  1. Create a variable to store an ArcGIS Online portal item ID. This is the unique ID for the Los Angeles Trailheads portal item (feature layer).

    string portalItemId = "883cedb8c9fe4524b64d47666ed234a7";
    
  2. Create the portal item from the portal and the item ID:

    PortalItem portalItem = await PortalItem.CreateAsync(portal, portalItemId);
    

Create the layer from the portal item

  1. Create the layer using the portal item and its layer id:

    FeatureLayer layer = new FeatureLayer(portalItem, 0);
    

Add the layer to the map

  1. Ensure the layer is loaded and use it to update the map's InitialViewpoint:

    await layer.LoadAsync();
    Map = new Map(Basemap.CreateTopographic()) { InitialViewpoint = new Viewpoint(layer.FullExtent) };
    
  2. Update the MapViewModel.Map's OperationalLayers property with the newly created layer:

    Map.OperationalLayers.Add(layer);
    
  3. Update the MapViewModel constructor to call InitializeMap:

    public MapViewModel()
    {
        InitializeMap();
    }
    
  4. Run your app to test your code. When the app opens, you should see a map with the trailhead layer for Los Angeles.

Congratulations, you're done!

Your map and layer should load and display with the view point centered on the Los Angeles area and points identifying the trailheads.

Challenge

Use your portal item

Instead of using the portal item we used above, use the one you made in the data lab Create a new dataset.

In Step 6 above, replace the value of portalItemId with the id of your portal item.

Find the portal item URL

There are multiple ways to load a feature layer from ArcGIS Online. One way is to load the feature layer directly from a URL. This challenge is a follow-on to the challenge in Create a new dataset design lab.

Navigate to the page in ArcGIS Online for your portal item. In the item page, find the Service URL link and identify the service endpoint. This is the URL the runtime uses to access the data. It should look something like this:

https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Griffith_Park_Access/FeatureServer/0

Store the URL in a Uri:

// Hold the URL to the service
Uri uri = new Uri("https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Griffith_Park_Access/FeatureServer/0");

Create the FeatureLayer from the Uri

// Create the layer
FeatureLayer layer = new FeatureLayer(uri);

Add the layer to the map

Map.OperationalLayers.Add(layer);

Discover data

If you have not already done so, visit the Discover data lab and search for interesting open data you could add in another layer in your app.