【问题标题】:How can I remove all annotations from a map using MapKit如何使用 MapKit 从地图中删除所有注释
【发布时间】:2021-02-04 18:41:46
【问题描述】:

我尝试过使用 RemoveAnnotation 功能,但我不知道如何将它们全部删除

struct MapView: View {
    @State private var region = MKCoordinateRegion(
        center: CLLocationCoordinate2D(
            latitude: 25.7617,
            longitude: 80.1918
        ),
        span: MKCoordinateSpan(
            latitudeDelta: 100,
            longitudeDelta: 100
        )
    )
    

    var body: some View {
        Map(coordinateRegion: $region)
    } }

【问题讨论】:

    标签: swiftui mapkit


    【解决方案1】:

    我正在使用此扩展程序

    extension MKMapView {
            
            
            func removeAnnotationAndOverlay(annotation: MKAnnotation) {
                
                removeAnnotation(annotation)
                
                if overlays.count > 0 {
                    
                    if let overlay = overlays.first {
                        
                        removeOverlay(overlay)
                        
                    }
                    
                }
               
            }
            
            func removeAllOverlays() {
                
               
                removeOverlays(overlays)
                        
         
                
            }
            
            
            func removeAllAnnotations() {
                
                   
                removeAnnotations(annotations)
                    
        
            }
            
            func removeAllAnnotationsAndOverlays() {
                
                removeAllOverlays()
                
                removeAllAnnotations()
            }
        }
    

    【讨论】:

    • 我应该在哪里调用函数?
    • 从你的代码很难说。我使用 MapView 作为 UIViewRepresentable,makeUIView 返回实际的 Map 类,在我的例子中是 RideMapView 类:MKMapView、MKMapViewDelegate。然后在 updateUIView 我放置和删除注释。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-07
    • 2010-12-24
    相关资源
    最近更新 更多