Hello everyone,
I have just started coding using swift and I´m currently building an app that ist using MapKit. It is required to run on iOS 14 and newer and I want to add a Map switcher to switch between the Map Views Standard, Satellite, Hybrid and eventually also OSM. However this apparently is not as straight forward as it seems and I just don't get it to work. I had multiple attempts such as these two, each interface with a separate MapSwitcherView that open on the press of a button:
var body: some View {
ZStack(alignment: .bottomTrailing) {
Group {
if selectedMapStyle == .openStreetMap {
openStreetMapView()
} else {
MapContainer(region: $locationManager.region, tracking: $tracking, style: selectedMapStyle)
}
}
.id(selectedMapStyle)
.onChange(of: selectedMapStyle) { newStyle in
print("Style changed to: (newStyle)")
}
and
Group {
switch selectedMapStyle {
case .standard:
Map(coordinateRegion: $locationManager.region,
interactionModes: .all,
showsUserLocation: true,
userTrackingMode: $tracking)
.id("standard")
case .satellite:
Map(coordinateRegion: $locationManager.region,
interactionModes: .all,
showsUserLocation: true,
userTrackingMode: $tracking)
.id("satellite")
case .hybrid:
Map(coordinateRegion: $locationManager.region,
interactionModes: .all,
showsUserLocation: true,
userTrackingMode: $tracking)
.id("hybrid")
case .openStreetMap:
openStreetMapView()
}
}
Unfortunately the map just doesn't switch. Do you have any suggestions? Should I post some more code of the MapSwitcher or something?
Thanks and best regards
Selecting any option will automatically load the page