Display dimension features from a mobile map package.
Use case
Dimensions show specific lengths or distances on a map. A dimension may indicate the length of a land parcel, or the distance between two features, such as a fire hydrant and the corner of a building.
How to use the sample
When the sample loads, it will automatically display the map containing dimension features from the mobile map package. Control the visibility of the dimension layer and apply a definition expression to show dimensions greater than or equal to 450m in length using the toggles.
Note: The minimum scale range of the sample is set to 1:40,000 to maintain readability of the dimension features.
How it works
- Create a
MobileMapPackage
specifying the path to the .mmpk file. - Load the mobile map package (mmpk).
- After the mmpk successfully loads, get the map from the mmpk and add it to the map view.
- Loop through the map's layers to find the
DimensionLayer
. - Control the dimension layer's visibility with the
isVisible
property and set a definition expression using thedefinitionExpression
property.
Relevant API
- DimensionLayer
- MobileMapPackage
About the data
This sample shows a subset of the network of pylons, substations, and power lines around Edinburgh, Scotland within a Edinburgh Pylon Dimensions mobile map package. The data was digitized from satellite imagery and is intended to be used for illustrative purposes only.
Additional information
Dimension layers can be taken offline from a feature service hosted on ArcGIS Enterprise 10.9 or later, using the GeodatabaseSyncTask. Dimension layers are also supported in mobile map packages or mobile geodatabases created in ArcGIS Pro 2.9 or later.
Tags
definition expression, dimension, distance, layer, length, mmpk, mobile map package, utility
Sample Code
// Copyright 2023 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.
import ArcGIS
import SwiftUI
struct DisplayDimensionsView: View {
/// A map with no specified style.
@State private var map = Map()
/// The dimensional layer added to the map.
@State private var dimensionLayer: DimensionLayer?
/// A Boolean value indicating whether the dimension layer's content is visible.
@State private var dimensionLayerIsVisible = true
/// A Boolean value indicating whether the dimension layer's definition expression is set.
@State private var definitionExpressionIsSet = false
/// The error shown in the error alert.
@State private var error: Error?
var body: some View {
MapView(map: map)
.task {
do {
try await loadDimensionLayer()
} catch {
self.error = error
}
}
.overlay {
VStack {
Spacer()
Group {
Toggle("Dimension Layer", isOn: $dimensionLayerIsVisible)
.onChange(of: dimensionLayerIsVisible) { newValue in
dimensionLayer?.isVisible = newValue
}
Toggle(
"Definition Expression:\nDimensions >= 450m",
isOn: $definitionExpressionIsSet
)
.onChange(of: definitionExpressionIsSet) { newValue in
dimensionLayer?.definitionExpression = newValue ? "DIMLENGTH >= 450" : ""
}
}
.padding(8)
.background(.ultraThinMaterial)
.clipShape(.rect(cornerRadius: 10))
.disabled(dimensionLayer == nil)
}
.padding()
.padding(.bottom)
}
.errorAlert(presentingError: $error)
}
}
private extension DisplayDimensionsView {
/// Loads a map with a dimension layer from a local mobile map package.
private func loadDimensionLayer() async throws {
// Load the local mobile map package using a URL.
let mapPackage = MobileMapPackage(fileURL: .edinburghPylonDimensions)
try await mapPackage.load()
// Set the map to the first map in the mobile map package.
if let map = mapPackage.maps.first {
// Set the minScale to maintain dimension readability.
map.minScale = 4e4
self.map = map
} else {
throw SetupError.noMap
}
// Set the dimension layer using the one on the map.
if let layer = map.operationalLayers.first(where: { $0 is DimensionLayer }) as? DimensionLayer {
dimensionLayer = layer
} else {
throw SetupError.noDimensionLayer
}
}
/// The errors for the sample that can be thrown during setup.
private enum SetupError: String, LocalizedError {
case noMap = "The MMPK does not contain a map."
case noDimensionLayer = "The map does not contain a dimension layer."
/// The text description of the error.
var errorDescription: String? {
NSLocalizedString(
self.rawValue,
comment: "Error thrown when the setup for the sample fails."
)
}
}
}
private extension URL {
/// The URL to the local Edinburgh Pylon Dimensions mobile map package file.
static var edinburghPylonDimensions: URL {
Bundle.main.url(forResource: "Edinburgh_Pylon_Dimensions", withExtension: "mmpk")!
}
}