-
Notifications
You must be signed in to change notification settings - Fork 520
/
ShowLabelsOnLayer.xaml.cs
101 lines (84 loc) · 4.58 KB
/
ShowLabelsOnLayer.xaml.cs
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
97
98
99
100
101
// Copyright 2022 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: http://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.
using Esri.ArcGISRuntime.Data;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Mapping.Labeling;
using Esri.ArcGISRuntime.Symbology;
using Color = System.Drawing.Color;
namespace ArcGIS.Samples.ShowLabelsOnLayer
{
[ArcGIS.Samples.Shared.Attributes.Sample(
name: "Show labels on layers",
category: "Layers",
description: "Display custom labels on a feature layer.",
instructions: "Pan and zoom around the United States. Labels for congressional districts will be shown in red for Republican districts and blue for Democrat districts. Notice how labels pop into view as you zoom in.",
tags: new[] { "arcade", "attribute", "deconfliction", "label", "labeling", "string", "symbol", "text", "visualization" })]
public partial class ShowLabelsOnLayer : ContentPage
{
public ShowLabelsOnLayer()
{
InitializeComponent();
// Create the UI, setup the control references and execute initialization
_ = Initialize();
}
private async Task Initialize()
{
// Create a map with a light gray canvas basemap.
Map sampleMap = new Map(BasemapStyle.ArcGISLightGray);
// Assign the map to the MapView.
MyMapView.Map = sampleMap;
// Define the URL string for the feature layer.
string layerUrl = "https://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/USA_115th_Congressional_Districts/FeatureServer/0";
// Create a service feature table from the URL.
ServiceFeatureTable featureTable = new ServiceFeatureTable(new Uri(layerUrl));
// Create a feature layer from the service feature table.
FeatureLayer districtFeatureLabel = new FeatureLayer(featureTable);
// Add the feature layer to the operations layers collection of the map.
sampleMap.OperationalLayers.Add(districtFeatureLabel);
try
{
// Load the feature layer - this way we can obtain it's extent.
await districtFeatureLabel.LoadAsync();
// Zoom the map view to the extent of the feature layer.
await MyMapView.SetViewpointCenterAsync(new MapPoint(-10846309.950860, 4683272.219411, SpatialReferences.WebMercator), 20000000);
// create label definitions for each party.
LabelDefinition republicanLabelDefinition = MakeLabelDefinition("Republican", System.Drawing.Color.Red);
LabelDefinition democratLabelDefinition = MakeLabelDefinition("Democrat", Color.Blue);
// Add the label definition to the feature layer's label definition collection.
districtFeatureLabel.LabelDefinitions.Add(republicanLabelDefinition);
districtFeatureLabel.LabelDefinitions.Add(democratLabelDefinition);
// Enable the visibility of labels to be seen.
districtFeatureLabel.LabelsEnabled = true;
}
catch (Exception ex)
{
await Application.Current.MainPage.DisplayAlert("Error", ex.Message, "OK");
}
}
private LabelDefinition MakeLabelDefinition(string partyName, Color color)
{
// Create a text symbol for styling the label.
TextSymbol textSymbol = new TextSymbol
{
Size = 12,
Color = color,
HaloColor = Color.White,
HaloWidth = 2,
};
// Create a label expression using an Arcade expression script.
LabelExpression arcadeLabelExpression = new ArcadeLabelExpression("$feature.NAME + \" (\" + left($feature.PARTY,1) + \")\\nDistrict \" + $feature.CDFIPS");
return new LabelDefinition(arcadeLabelExpression, textSymbol)
{
Placement = Esri.ArcGISRuntime.ArcGISServices.LabelingPlacement.PolygonAlwaysHorizontal,
WhereClause = $"PARTY = '{partyName}'",
};
}
}
}