You will learn: how to build an app that displays feature layers in a 2D map.
With ArcGIS Runtime SDK for .NET, it's easy to create a mapping app that displays a basemap and one or more layers. ArcGIS Online hosts several basemaps (in both image and vector tile format) that you can use in your map (2D) or scene (3D). Feature layers allow you to add datasets hosted online (with ArcGIS Online or other ArcGIS servers) or stored locally (in a runtime geodatabase, for example). The data returned and the symbology used can be customized for a feature layer.
In this tutorial you will create a new map with an imagery basemap, add a feature layer to it, and display the map in your app.
Start Visual Studio.
Choose File > New > Project and select the ArcGIS Runtime app (WPF .NET Framework) template for your chosen platform.
Open the MapViewModel.cs code file in your project. Find the definition for the Map property in the class. Notice that the default map is currently set to display the streets vector tile basemap.
Add the following function to the MapViewModel class:
private async void CreateNewMap()
{
}
Add code to create a new Map
with an imagery basemap. In the following steps, add your code inside the CreateNewMap()
function you created above.
Map newMap = new Map(Basemap.CreateImageryWithLabels());
Add code that creates a new FeatureLayer
. For the layer data source, provide a URI to the Trailheads layer in the Los Angeles Trails map service.
FeatureLayer trailHeadsLayer = new FeatureLayer(new Uri("https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trailheads/FeatureServer/0"));
Load the layer asynchronously and await its completion.
await trailHeadsLayer.LoadAsync();
Add the layer to the map's collection of operational layers.
newMap.OperationalLayers.Add(trailHeadsLayer);
Set the initial display area (Viewpoint
) of the map to the extent of features in the trailheads layer.
newMap.InitialViewpoint = new Viewpoint(trailHeadsLayer.FullExtent);
Set the MapViewModel.Map property with your new map.
Map = newMap;
Call your CreateNewMap function from the MapViewModel constructor.
public MapViewModel()
{
CreateNewMap();
}
Run your app to test your code. When the app opens, you should see an imagery map of Los Angeles appear with trailhead points displayed on top.
Check out and compare with our completed solution project.
Look at the available static methods on the Basemap
class for creating basemaps for your Map
. Modify your code to display a different basemap. For example:
Map newMap = new Map(Basemap.CreateNationalGeographic());
Visit ArcGIS Online and find additional interesting park datasets to load into the app. Here are a couple suggestions:
https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Trails/FeatureServer/0
https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/Parks_and_Open_Space/FeatureServer/0
Sometimes you want your layer to show a subset of the data from a service. A definition expression lets you define attribute criteria that restrict the features for a layer. Consult the service info for the Trailheads data source and construct an expression for the layer's DefinitionExpression
property that will show only trailheads without parking.