Skip to content

Commit ea78476

Browse files
authored
Merge pull request #530 from Esri/Caleb/Update-ShowGrid
[Update] `Show grid` to include scene view
2 parents 9e66d3e + f1e596e commit ea78476

File tree

3 files changed

+77
-28
lines changed

3 files changed

+77
-28
lines changed

Shared/Samples/Show grid/README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,33 @@
11
# Show grid
22

3-
Display coordinate system grids including latitude-longitude, MGRS, UTM and USNG on a map view. Also, toggle label visibility and change the color of grid lines and grid labels.
3+
Display and customize coordinate system grids, including Latitude/Longitude, MGRS, UTM, and USNG, on a map view or scene view.
44

55
![Image of Show grid sample](show-grid.png)
66

77
## Use case
88

9-
Grids are often used on printed maps, but can also be helpful on digital maps, to identify locations on a map.
9+
Grids are often used on printed maps, but can also be helpful on digital 2D maps or 3D scenes, to identify locations.
1010

1111
## How to use the sample
1212

13-
Tap the button in the toolbar to open the grid settings view. You can select type of grid from grid types (LatLong, MGRS, UTM and USNG) and modify its properties like grid visibility, grid color, label visibility, label color, label position, label format and label unit.
13+
Use the picker to change the view from 2D or 3D, or tap the button in the toolbar to open the grid settings. You can select the type of grid (LatLong, MGRS, UTM, and USNG) and modify its properties like the visibility and color of the lines, and the position, format, and units of the labels.
1414

1515
## How it works
1616

1717
1. Create an instance of one of the `Grid` types.
1818
2. Grid lines and labels can be styled per grid level with `grid.lineSymbols[0]` and `grid.textSymbols[0]` subscripts on the grid.
19-
3. The label position, format, unit and visibility can be specified with `labelPosition`, `labelFormat`, `labelUnit` and `isVisible` on the `Grid`.
19+
3. The label position, format, unit, and visibility can be specified with `labelPosition`, `labelFormat`, `labelUnit`, and `isVisible` on the `Grid`.
20+
* Note that as of 200.6, MGRS, UTM, and USNG grids in a SceneView only support the `geographic` label position.
2021
4. For the `LatitudeLongitudeGrid` type, you can specify a label format of `decimalDegrees` or `degreesMinutesSeconds`.
21-
5. To set the grid, assign it to the map view using the `grid(_:)` modifier.
22+
5. To set the grid, assign it to the map view or scene view using the `grid(_:)` modifier.
2223

2324
## Relevant API
2425

2526
* Grid
2627
* LatitudeLongitudeGrid
2728
* MapView
2829
* MGRSGrid
30+
* SceneView
2931
* SimpleLineSymbol
3032
* TextSymbol
3133
* USNGGrid

Shared/Samples/Show grid/README.metadata.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"category": "Maps",
3-
"description": "Display coordinate system grids including latitude-longitude, MGRS, UTM and USNG on a map view. Also, toggle label visibility and change the color of grid lines and grid labels.",
2+
"category": "Visualization",
3+
"description": "Display and customize coordinate system grids, including Latitude/Longitude, MGRS, UTM, and USNG, on a map view or scene view.",
44
"ignore": false,
55
"images": [
66
"show-grid.png"
@@ -21,6 +21,7 @@
2121
"LatitudeLongitudeGrid",
2222
"MGRSGrid",
2323
"MapView",
24+
"SceneView",
2425
"SimpleLineSymbol",
2526
"TextSymbol",
2627
"USNGGrid",
@@ -32,6 +33,7 @@
3233
"LatitudeLongitudeGrid",
3334
"MGRSGrid",
3435
"MapView",
36+
"SceneView",
3537
"SimpleLineSymbol",
3638
"TextSymbol",
3739
"USNGGrid",

Shared/Samples/Show grid/ShowGridView.swift

Lines changed: 66 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,44 @@ struct ShowGridView: View {
1919
/// The view model for the sample.
2020
@StateObject private var model = Model()
2121

22+
/// The current viewpoint of the geo views.
23+
@State private var viewpoint = Viewpoint(latitude: 34.05, longitude: -118.25, scale: 8e6)
24+
2225
/// A Boolean value indicating whether the settings view should be presented.
2326
@State private var showsGridSettingsView = false
2427

2528
var body: some View {
26-
MapView(map: model.map)
27-
.grid(model.grid)
28-
.toolbar {
29-
ToolbarItem(placement: .bottomBar) {
30-
Button("Grid Settings") {
31-
showsGridSettingsView = true
32-
}
33-
.popover(isPresented: $showsGridSettingsView) {
34-
NavigationStack {
35-
GridSettingsView(model: model)
36-
}
37-
.presentationDetents([.fraction(0.6), .large])
38-
.frame(idealWidth: 350, idealHeight: 480)
29+
Group {
30+
switch model.geoViewType {
31+
case .mapView:
32+
MapView(map: model.map, viewpoint: viewpoint)
33+
.grid(model.grid)
34+
.onViewpointChanged(kind: .centerAndScale) { viewpoint = $0 }
35+
case .sceneView:
36+
SceneView(scene: model.scene, viewpoint: viewpoint)
37+
.grid(model.grid)
38+
.onViewpointChanged(kind: .centerAndScale) { viewpoint = $0 }
39+
}
40+
}
41+
.toolbar {
42+
ToolbarItemGroup(placement: .bottomBar) {
43+
Button("Grid Settings") {
44+
showsGridSettingsView = true
45+
}
46+
.popover(isPresented: $showsGridSettingsView) {
47+
NavigationStack {
48+
GridSettingsView(model: model)
3949
}
50+
.presentationDetents([.fraction(0.6), .large])
51+
.frame(idealWidth: 350, idealHeight: 480)
52+
}
53+
54+
Picker("Geo View", selection: $model.geoViewType) {
55+
Text("Map View").tag(GeoViewType.mapView)
56+
Text("Scene View").tag(GeoViewType.sceneView)
4057
}
4158
}
59+
}
4260
}
4361
}
4462

@@ -47,14 +65,23 @@ private extension ShowGridView {
4765

4866
/// The view model for the sample.
4967
final class Model: ObservableObject {
50-
/// A map with topographic basemap.
51-
let map: Map = {
52-
let map = Map(basemapStyle: .arcGISTopographic)
53-
map.initialViewpoint = Viewpoint(latitude: 34.05, longitude: -118.25, scale: 8e6)
54-
return map
68+
/// A map with a topographic basemap.
69+
let map = Map(basemapStyle: .arcGISTopographic)
70+
71+
/// A scene with elevation and a topographic basemap.
72+
let scene: ArcGIS.Scene = {
73+
let scene = Scene(basemapStyle: .arcGISTopographic)
74+
let elevationSource = ArcGISTiledElevationSource(url: .worldElevationService)
75+
scene.baseSurface.addElevationSource(elevationSource)
76+
return scene
5577
}()
5678

57-
/// The map view's grid, initially set to a Lat-Lon grid.
79+
/// The type of geo view that is showing.
80+
@Published var geoViewType = GeoViewType.mapView {
81+
didSet { grid = makeGrid(type: gridType) }
82+
}
83+
84+
/// The geo view's grid, initially set to a Lat-Lon grid.
5885
@Published var grid: ArcGIS.Grid = LatitudeLongitudeGrid()
5986

6087
/// The kind of grid to display.
@@ -71,6 +98,11 @@ private extension ShowGridView {
7198
/// The units used for labeling the MGRS grid.
7299
@Published var mgrsLabelUnit: MGRSGrid.LabelUnit = .kilometersMeters
73100

101+
/// A Boolean value indicating whether the current grid only supports `LabelPosition.geographic`.
102+
var gridOnlySupportsGeographic: Bool {
103+
geoViewType == .sceneView && gridType != .latitudeLongitude
104+
}
105+
74106
/// Creates a new grid of a given type.
75107
/// - Parameter gridType: The kind of grid to make.
76108
/// - Returns: A new `Grid` object.
@@ -97,12 +129,17 @@ private extension ShowGridView {
97129
newGrid.labelsAreVisible = grid.labelsAreVisible
98130
newGrid.linesColor = grid.linesColor
99131
newGrid.labelsColor = grid.labelsColor
100-
newGrid.labelPosition = grid.labelPosition
132+
newGrid.labelPosition = gridOnlySupportsGeographic ? .geographic : grid.labelPosition
101133

102134
return newGrid
103135
}
104136
}
105137

138+
/// A type of `GeoView`.
139+
enum GeoViewType {
140+
case mapView, sceneView
141+
}
142+
106143
// MARK: - Settings View
107144

108145
struct GridSettingsView: View {
@@ -136,6 +173,7 @@ private extension ShowGridView {
136173
Text(position.label)
137174
}
138175
}
176+
.disabled(model.gridOnlySupportsGeographic)
139177

140178
if let latitudeLongitudeGrid = model.grid as? LatitudeLongitudeGrid {
141179
Picker("Format", selection: $model.labelFormat) {
@@ -211,7 +249,7 @@ private extension ArcGIS.Grid {
211249
}
212250

213251
private extension ShowGridView {
214-
/// The kinds of grid to show in a map view.
252+
/// The kinds of grid to show on the geo view.
215253
enum GridType: CaseIterable {
216254
case latitudeLongitude, mgrs, usng, utm
217255

@@ -289,6 +327,13 @@ private extension USNGGrid.LabelUnit {
289327
}
290328
}
291329

330+
private extension URL {
331+
/// A web URL to the Terrain3D image server on ArcGIS REST.
332+
static var worldElevationService: URL {
333+
URL(string: "https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer")!
334+
}
335+
}
336+
292337
// MARK: - Preview
293338

294339
#Preview {

0 commit comments

Comments
 (0)