SwiftUI MapKit - MapAnnotation -导致如此多的视图更新错误-我们可以在生产版本中使用它吗?



我想在SwiftUI中使用MapKit和MapAnnotation。因此,我可以将自定义视图作为地图引脚,并在稍后添加点击手势。

但是当我运行应用程序时,Xcode开始显示很多UI错误:

不允许在视图更新中发布更改,这将导致未定义的行为。

代码是我们所能找到的最简单的,即使是在苹果文档中或下面的那些来自swift的黑客

import SwiftUI
import MapKit
struct Location: Identifiable {
let id = UUID()
let name: String
let coordinate: CLLocationCoordinate2D
}
struct MapView: View {
@State private var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 51.5, longitude: -0.12), span: MKCoordinateSpan(latitudeDelta: 0.2, longitudeDelta: 0.2))

let locations = [
Location(name: "Buckingham Palace", coordinate: CLLocationCoordinate2D(latitude: 51.501, longitude: -0.141)),
Location(name: "Tower of London", coordinate: CLLocationCoordinate2D(latitude: 51.508, longitude: -0.076))
]

var body: some View {
Map(coordinateRegion: $region, annotationItems: locations) { location in
MapAnnotation(coordinate: location.coordinate) {
Circle()
.stroke(.red, lineWidth: 3)
.frame(width: 44, height: 44)
}
}
.navigationTitle("Map")
.edgesIgnoringSafeArea(.all)
}
}

你有什么建议或解决方法吗?

我们可以在生产版本中使用具有这些巨大错误的MapKit吗?

?

更新

根据最近的回答,作为一个解决方案,使用特定的Binding<MKCoordinateRegion>()coordinateRegion,删除警告/错误。

但是如果你想根据所选的引脚移动地图,这些错误/警告将再次出现。

struct MapView: View {
@State private var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 51.5, longitude: -0.12), span: MKCoordinateSpan(latitudeDelta: 0.2, longitudeDelta: 0.2))

let locations = [
Location(name: "Buckingham Palace", coordinate: CLLocationCoordinate2D(latitude: 51.501, longitude: -0.141)),
Location(name: "Tower of London", coordinate: CLLocationCoordinate2D(latitude: 51.508, longitude: -0.076))
]

@State var selectedLocation: Location?
var body: some View {
ZStack {
Map(coordinateRegion: Binding<MKCoordinateRegion>(
get: { region },
set: { _ in }
), annotationItems: locations) { location in
MapAnnotation(coordinate: location.coordinate) {
Circle()
.stroke(.red, lineWidth: 3)
.frame(width: 44, height: 44)
.onTapGesture {
selectedLocation = location
}
}
}
}
.onChange(of: selectedLocation) { newValue in
if let location = newValue {
withAnimation {
region = MKCoordinateRegion(center: location.coordinate, span: MKCoordinateSpan(latitudeDelta: 0.2, longitudeDelta: 0.2))
}
}
}
.edgesIgnoringSafeArea(.all)
}
}

作为解决方案,使用特定的Binding<MKCoordinateRegion>()coordinateRegion,为我删除警告/错误。

struct MapView: View {
@State var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 51.5, longitude: -0.12), span: MKCoordinateSpan(latitudeDelta: 0.2, longitudeDelta: 0.2))

let locations = [
Location(name: "Buckingham Palace", coordinate: CLLocationCoordinate2D(latitude: 51.501, longitude: -0.141)),
Location(name: "Tower of London", coordinate: CLLocationCoordinate2D(latitude: 51.508, longitude: -0.076))
]
var body: some View {
Map(coordinateRegion: Binding<MKCoordinateRegion>(
get: { region },
set: { _ in }
), annotationItems: locations) { location in
MapAnnotation(coordinate: location.coordinate) {
Circle().stroke(.red, lineWidth: 3).frame(width: 44, height: 44)
}
}
.navigationTitle("Map")
.edgesIgnoringSafeArea(.all)
}
}

最新更新