Post

Replies

Boosts

Views

Activity

Reply to Performance issues using MapKit for SwiftUI
The minimal reproducible example: import SwiftUI import MapKit extension CLLocationCoordinate2D: Identifiable, Hashable { public static func == (lhs: CLLocationCoordinate2D, rhs: CLLocationCoordinate2D) -> Bool { lhs.latitude == rhs.latitude && lhs.longitude == rhs.longitude } public func hash(into hasher: inout Hasher) { hasher.combine(self.id) } public var id: String { String(self.latitude) + " " + String(self.longitude) } } struct MapKitIssuesView: View { @State var coordinates: [CLLocationCoordinate2D] = [] @State var selectedCoord: CLLocationCoordinate2D? var body: some View { ZStack { Map { ForEach(coordinates, id: \.self) { coord in let _ = print("reloaded \(coord) at \(Date())") Annotation("", coordinate: coord) { Circle() .fill(.black) .frame(width: 4, height: 4) } } if let selectedCoord { Annotation("", coordinate: selectedCoord) { Circle() .stroke(.orange, lineWidth: 4) .fill(.white) .frame(width: 16, height: 16) } } else { EmptyMapContent() } } .mapStyle(.standard(pointsOfInterest: .excludingAll)) .onAppear { for lat in 30 ... 45 { for lon in -137 ... -79 { coordinates.append(.init(latitude: Double(lat), longitude: Double(lon))) } } } .onDisappear { coordinates.removeAll() } VStack { Spacer() Button(action: { selectedCoord = .init(latitude: .random(in: 30 ..< 45), longitude: .random(in: -137 ..< -79)) }, label: { Text("Change Selection") }) .tint(.white) } } } } #Preview { MapKitIssuesView() }
Oct ’23