Skip to content

Commit 6fccec1

Browse files
committed
Add source code.
1 parent 1036d48 commit 6fccec1

File tree

1 file changed

+57
-3
lines changed

1 file changed

+57
-3
lines changed

Shared/Samples/Apply dictionary renderer to feature layer/ApplyDictionaryRendererToFeatureLayerView.swift

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,60 @@ import ArcGIS
1616
import SwiftUI
1717

1818
struct ApplyDictionaryRendererToFeatureLayerView: View {
19+
/// A map with a topographic basemap.
1920
@State private var map = Map(basemapStyle: .arcGISTopographic)
2021

22+
/// The viewpoint for zooming the map view to the feature layers.
23+
@State private var viewpoint: Viewpoint?
24+
25+
/// The error shown in the error alert.
26+
@State private var error: Error?
27+
2128
var body: some View {
22-
MapView(map: map)
29+
MapView(map: map, viewpoint: viewpoint)
30+
.task {
31+
do {
32+
// Creates the feature layers and adds them to the map when the sample opens.
33+
let featureLayers = try await makeMIL2525DFeatureLayers()
34+
map.addOperationalLayers(featureLayers)
35+
36+
// Zooms the viewpoint to the feature layers using their extents.
37+
let layerExtents = featureLayers.compactMap(\.fullExtent)
38+
if let combinedExtents = GeometryEngine.combineExtents(of: layerExtents) {
39+
viewpoint = Viewpoint(boundingGeometry: combinedExtents)
40+
}
41+
} catch {
42+
self.error = error
43+
}
44+
}
45+
.errorAlert(presentingError: $error)
46+
}
47+
48+
/// Creates a list of feature layers with mil2525d symbols.
49+
/// - Returns: A list of new `FeatureLayer` objects.
50+
private func makeMIL2525DFeatureLayers() async throws -> [FeatureLayer] {
51+
// Creates and loads a geodatabase from a local file.
52+
let geodatabase = Geodatabase(fileURL: .militaryOverlayGeodatabase)
53+
try await geodatabase.load()
54+
55+
// Creates and loads a mil2525d dictionary symbol style from a local file.
56+
let mil2525dDictionarySymbolStyle = DictionarySymbolStyle(url: .mil2525dStyleFile)
57+
try await mil2525dDictionarySymbolStyle.load()
58+
59+
// Creates feature layers from the geodatabase's feature tables.
60+
let featureLayers = geodatabase.featureTables.map { featureTable in
61+
let featureLayer = FeatureLayer(featureTable: featureTable)
62+
63+
// Sets the layer's renderer to display the features using mil2525d symbols.
64+
featureLayer.renderer = DictionaryRenderer(
65+
dictionarySymbolStyle: mil2525dDictionarySymbolStyle
66+
)
67+
featureLayer.minScale = 1000000
68+
return featureLayer
69+
}
70+
await featureLayers.load()
71+
72+
return featureLayers
2373
}
2474
}
2575

@@ -29,8 +79,12 @@ private extension URL {
2979
Bundle.main.url(forResource: "mil2525d", withExtension: "stylx")!
3080
}
3181

32-
/// The URL to the local "Military Overlay" geodatabase.
82+
/// The URL to the local "Military Overlay" geodatabase file.
3383
static var militaryOverlayGeodatabase: URL {
34-
Bundle.main.url(forResource: "militaryoverlay", withExtension: ".geodatabase")!
84+
Bundle.main.url(
85+
forResource: "militaryoverlay",
86+
withExtension: "geodatabase",
87+
subdirectory: "militaryoverlay.geodatabase"
88+
)!
3589
}
3690
}

0 commit comments

Comments
 (0)