It would be great if adding an overview map were simpler. One way of doing that would be to have a custom view modifier for the overview map in the toolkit:
MapView(map: map)
.overviewMap(alignment: .topTrailing)
A proof of concept is as follows:
extension MapView {
func overviewMap(alignment: Alignment) -> some View {
MapViewWithOverviewMap(mapView: self, alignment: alignment)
}
}
struct MapViewWithOverviewMap: View {
let mapView: MapView
let alignment: Alignment
@State private var viewpoint: Viewpoint?
@State private var visibleArea: Polygon?
var body: some View {
mapView
.onViewpointChanged(kind: .centerAndScale) { viewpoint = $0 }
.onVisibleAreaChanged { visibleArea = $0 }
.overlay(alignment: alignment) {
OverviewMap.forMapView(
with: viewpoint,
visibleArea: visibleArea
)
.frame(width: 200, height: 132)
.padding()
}
}
}
As a proof of concept, it isn't without problems. We would need to change how those modifiers on map/scene view are implemented since users may also want to use them themselves (currently each subsequent use would override the previous).
The overview map could be customized with an overviewMapStyle(_:) modifiers, similar to other SwiftUI views. This should be explored as part of this issue; the OverviewMap
already provides modifiers for symbol
, map
, and scaleFactor
. Should they be combined into one overviewMapStyle(map:, symbol:, scaleFactor:)
?
To get around the .onViewpointChanged(kind:)/.onVisibleAreaChanged() issue, maybe the .overviewMap(alignment:) modifier could take an optional binding to a Viewpoint and visible area Polygon and then update it alongside the local viewpoint and visible area.
Thanks to @philium for the suggestion!