Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mhd/overview map refactor #41

Merged
merged 6 commits into from
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 20 additions & 19 deletions Examples/Examples/OverviewMapExampleView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,35 @@ struct OverviewMapExampleView: View {
case scene
}

@State
private var mapOrScene: MapOrScene = .map
@State private var mapOrScene: MapOrScene = .map

var body: some View {
Picker("Map or Scene", selection: $mapOrScene, content: {
Text("Map").tag(MapOrScene.map)
Text("Scene").tag(MapOrScene.scene)
})
.pickerStyle(SegmentedPickerStyle())
.padding()
switch mapOrScene {
case .map:
OverviewMapForMapView()
case .scene:
OverviewMapForSceneView()
Group {
switch mapOrScene {
case .map:
OverviewMapForMapView()
case .scene:
OverviewMapForSceneView()
}
}
.toolbar(content: {
ToolbarItem(placement: .navigationBarTrailing) {
Picker("Map or Scene", selection: $mapOrScene) {
Text("Map").tag(MapOrScene.map)
Text("Scene").tag(MapOrScene.scene)
}
.pickerStyle(.menu)
}
})
}
}

struct OverviewMapForMapView: View {
let map = Map(basemapStyle: .arcGISImagery)

@State
private var viewpoint: Viewpoint?
@State private var viewpoint: Viewpoint?

@State
private var visibleArea: ArcGIS.Polygon?
@State private var visibleArea: ArcGIS.Polygon?

var body: some View {
MapView(map: map)
Expand All @@ -75,8 +77,7 @@ struct OverviewMapForMapView: View {
struct OverviewMapForSceneView: View {
let scene = Scene(basemapStyle: .arcGISImagery)

@State
private var viewpoint: Viewpoint?
@State private var viewpoint: Viewpoint?

var body: some View {
SceneView(scene: scene)
Expand Down
62 changes: 32 additions & 30 deletions Sources/ArcGISToolkit/Components/OverviewMap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,13 @@ public struct OverviewMap: View {

private var scaleFactor = 25.0

@StateObject
private var map = Map(basemapStyle: .arcGISTopographic)
@StateObject private var map = Map(basemapStyle: .arcGISTopographic)

/// The `Graphic` displaying the visible area of the main `GeoView`.
@StateObject
private var graphic: Graphic
@StateObject private var graphic: Graphic

/// The `GraphicsOverlay` used to display the visible area graphic.
@StateObject
private var graphicsOverlay: GraphicsOverlay
@StateObject private var graphicsOverlay: GraphicsOverlay

/// Creates an `OverviewMap` for use on a `MapView`.
/// - Parameters:
Expand All @@ -60,7 +57,7 @@ public struct OverviewMap: View {
) -> OverviewMap {
OverviewMap(viewpoint: viewpoint, symbol: .defaultMarker)
}

/// Creates an `OverviewMap`. Used for creating an `OverviewMap` for use on a `MapView`.
/// - Parameters:
/// - viewpoint: Viewpoint of the main `GeoView` used to update the `OverviewMap` view.
Expand All @@ -75,7 +72,7 @@ public struct OverviewMap: View {
self.symbol = symbol

let graphic = Graphic(symbol: self.symbol)

// It is necessary to set the graphic and graphicsOverlay this way
// in order to prevent the main geoview from recreating the
// graphicsOverlay every draw cycle. That was causing refresh issues
Expand All @@ -90,33 +87,38 @@ public struct OverviewMap: View {
viewpoint: makeOverviewViewpoint(),
graphicsOverlays: [graphicsOverlay]
)
.attributionText(hidden: true)
.interactionModes([])
.border(.black, width: 1)
.onAppear(perform: {
graphic.symbol = symbol
})
.onChange(of: visibleArea, perform: { visibleArea in
if let visibleArea = visibleArea {
graphic.geometry = visibleArea
}
})
.onChange(of: viewpoint, perform: { viewpoint in
if visibleArea == nil,
let viewpoint = viewpoint,
let point = viewpoint.targetGeometry as? Point {
graphic.geometry = point
}
})
.onChange(of: symbol, perform: {
graphic.symbol = $0
})
.attributionText(hidden: true)
.interactionModes([])
.border(
.black,
width: 1
)
.onAppear {
graphic.symbol = symbol
}
.onChange(of: visibleArea) { visibleArea in
if let visibleArea = visibleArea {
graphic.geometry = visibleArea
}
}
.onChange(of: viewpoint) { viewpoint in
if visibleArea == nil,
let viewpoint = viewpoint,
let point = viewpoint.targetGeometry as? Point {
graphic.geometry = point
}
}
.onChange(of: symbol) {
graphic.symbol = $0
}
}

/// Creates an overview viewpoint based on the observed `viewpoint` center, scale, and `scaleFactor`.
/// - Returns: The new `Viewpoint`.
func makeOverviewViewpoint() -> Viewpoint? {
guard let viewpoint = viewpoint, let center = viewpoint.targetGeometry as? Point else { return nil }
guard let viewpoint = viewpoint,
let center = viewpoint.targetGeometry as? Point else { return nil }

return Viewpoint(
center: center,
scale: viewpoint.targetScale * scaleFactor
Expand Down