【发布时间】:2022-10-24 21:02:58
【问题描述】:
如果我使用默认的MapMarker,地图可以正常工作。但是,如果我使用简单的自定义MapAnnotation,地图会变得非常缓慢,并且当我在地图上移动时会无数次显示错误信息。
[SwiftUI] 不允许从视图更新中发布更改,这将导致未定义的行为”
我的代码如下:
struct UNESCOUIView: View { @EnvironmentObject private var UM: UNESCOModel @State var isShowingMapView = false var body: some View { NavigationView { //code } .sheet(isPresented: $isShowingMapView) { MapUNUIView(UNsites: $UM.UNESCOSites, isShowingMapView: $isShowingMapView) } } struct MapUNUIView: View { @Binding var UNsites:[UNESCOSite] @Binding var isShowingMapView: Bool @State private var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 51.507222, longitude: -0.1275), span: MKCoordinateSpan(latitudeDelta: 10, longitudeDelta: 10)) var body: some View { NavigationView { Map(coordinateRegion: $region, showsUserLocation: true, annotationItems: $UNsites) { $place in // works fine with this //MapMarker(coordinate: place.coordinate) // doesn't work with this MapAnnotation(coordinate: place.coordinate) { Circle() .strokeBorder(.red, lineWidth: 4) .frame(width: 40, height: 40) } }.ignoresSafeArea(.all) } }
【问题讨论】: