Apologies for that. The MapUserTrackingMode and UISegmentedControl Issue can be fixed by adding $ before the two related state variables in the code. Also, please make sure to add appropriate tag to each Text view in the Picker, for the segment value to be passed correctly as shown below:
import SwiftUI
import MapKit
struct MapView: View {
enum MapType: String, CaseIterable {
case standard = "Standard"
case satellite = "Satellite"
case hybrid = "Hybrid"
case satelliteFlyover = "SatelliteFlyover"
case hybridFlyover = "HybridFlyover"
case mutedStandard = "MutedStandard"
}
class CustomAnnotation: NSObject, MKAnnotation, Identifiable {
let id = UUID()
let title: String?
let subtitle: String?
let coordinate: CLLocationCoordinate2D
init(title: String?, subtitle: String?, coordinate: CLLocationCoordinate2D) {
self.title = title
self.subtitle = subtitle
self.coordinate = coordinate
}
}
@State private var region = MKCoordinateRegion(
center: CLLocationCoordinate2D(latitude: 37.331516, longitude: -122.030956),
span: MKCoordinateSpan(latitudeDelta: 0.1, longitudeDelta: 0.1)
)
@State private var mapType = MapType.standard
@State private var userTrackingMode: Binding<MapUserTrackingMode>? = .constant(.none) //fix
@State private var annotations: [CustomAnnotation] = []
func getMapType() -> MKMapType {
switch mapType {
case .standard:
return .standard
case .satellite:
return .satellite
case .hybrid:
return .hybrid
case .satelliteFlyover:
return .satelliteFlyover
case .hybridFlyover:
return .hybridFlyover
case .mutedStandard:
if #available(iOS 13.0, *) {
return .mutedStandard
} else {
return .standard
}
}
}
var body: some View {
ZStack(alignment: .trailing) {
Map(coordinateRegion: $region, interactionModes: .all, showsUserLocation: true, userTrackingMode: userTrackingMode, annotationItems: annotations) { annotation in
MapMarker(coordinate: annotation.coordinate, tint: .red)
}
.onAppear {
setMapType()
}
.onChange(of: mapType) { _ in
setMapType()
}
VStack {
Spacer()
Picker(selection: $mapType, label: Text("")) {
ForEach(MapType.allCases, id: \.self) { type in
Text(type.rawValue).tag(type) //fix
}
}
.pickerStyle(SegmentedPickerStyle())
.padding(.bottom, 10)
Button(action: {
if region.span.latitudeDelta > 0.02 && region.span.longitudeDelta > 0.02 {
region.span = MKCoordinateSpan(latitudeDelta: region.span.latitudeDelta / 2, longitudeDelta: region.span.longitudeDelta / 2)
}
}) {
Image(systemName: "plus")
.foregroundColor(.white)
.font(.largeTitle)
.padding()
.frame(width: 50, height: 50)
.background(Color.gray.opacity(0.5))
.cornerRadius(30)
}
Button(action: {
let newLatitudeDelta = region.span.latitudeDelta * 2
let newLongitudeDelta = region.span.longitudeDelta * 2
if newLatitudeDelta <= 180 && newLongitudeDelta <= 180 {
region.span = MKCoordinateSpan(latitudeDelta: newLatitudeDelta, longitudeDelta: newLongitudeDelta)
}
}) {
Image(systemName: "minus")
.foregroundColor(.white)
.font(.largeTitle)
.padding()
.frame(width: 50, height: 50)
.background(Color.gray.opacity(0.5))
.cornerRadius(30)
}
Spacer()
}
.padding(.trailing, 20)
}
.onAppear {
let customAnnotation1 = CustomAnnotation(title: "Title 1", subtitle: "Subtitle 1", coordinate: CLLocationCoordinate2D(latitude: 37.331516, longitude: -122.030956))
annotations.append(customAnnotation1)
let customAnnotation2 = CustomAnnotation(title: "Title 2", subtitle: "Subtitle 2", coordinate: CLLocationCoordinate2D(latitude: 37.431516, longitude: -122.030956))
annotations.append(customAnnotation2)
}
}
private func setMapType() {
let currentMapType = getMapType()
let mapTypeSegments = UISegmentedControl.appearance().subviews.map {
$0.accessibilityLabel = nil
return $0
}
if let mapTypeSegment = mapTypeSegments.enumerated().first(where: { $0.1.accessibilityIdentifier == currentMapType.rawValue }) {
UISegmentedControl.appearance().selectedSegmentIndex = mapTypeSegment.offset
}
}
}
struct MapView_Previews: PreviewProvider {
static var previews: some View {
MapView()
}
}
Please replace your code with the above code and check your Swift Playgrounds to see if the issue is resolved.
Let me know if you still face any issues.