【问题标题】:How can I show/hide the title of my MKPointAnnotation() pin after I set it inside of my UIViewRepresentable updateUIView (SwiftUI)?在我的 UIViewRepresentable updateUIView (SwiftUI) 中设置 MKPointAnnotation() 引脚后,如何显示/隐藏它的标题?
【发布时间】:2021-12-28 23:50:57
【问题描述】:

我想在点击图钉后切换 MKPointAnnotation 标题的可见性。我尝试直接在
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) 中更改标题,但它告诉我它只是一个 get 属性,我无法在我的 Coordinator 类中更改它。

任何帮助将不胜感激!

这里是相关代码...

import SwiftUI
import MapKit
import CoreLocationUI

struct MapViewTest: UIViewRepresentable {
    
    @EnvironmentObject var viewModel: MapViewModel
    
    @Binding var region: MKCoordinateRegion
    @Binding var lineCoordinates: [[CLLocationCoordinate2D]]
    
    func makeUIView(context: Context) -> MKMapView {
        let mapView = MKMapView()
        mapView.delegate = context.coordinator
        mapView.region = region
        mapView.showsUserLocation = true
        
        return mapView
    }
    
    func updateUIView(_ view: MKMapView, context: Context) {
        view.setRegion(region, animated: true)
        
        for i in viewModel.locations {
            let pin = MKPointAnnotation()
            pin.coordinate = i.coordinate
            pin.title = i.name
            view.addAnnotation(pin)
        }
        
        for i in lineCoordinates{
            let polyline = MKPolyline(coordinates: i, count: i.count)
            view.addOverlay(polyline)
        }
    }
    
    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }
    
    
}

class Coordinator: NSObject, MKMapViewDelegate {
        
    var parent: MapViewTest
    
    init(_ parent: MapViewTest) {
        self.parent = parent
    }
    
    func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
        
    }
    
    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
        if let routePolyline = overlay as? MKPolyline {
            let renderer = MKPolylineRenderer(polyline: routePolyline)
            renderer.strokeColor = UIColor.systemBlue
            renderer.lineWidth = 10
            return renderer
        }
        return MKOverlayRenderer()
    }
}

【问题讨论】:

  • 我对你的问题感到困惑。您的标题表示可见性,但您也谈到“更改标题”。是要显示和隐藏标题,还是要重命名注释的标题?
  • 抱歉,我想显示/隐藏标题。

标签: swiftui mapkit mkannotation uiviewrepresentable


【解决方案1】:

每当您制作 MapKit 注释时,您都应该包含 func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?。此功能允许您配置您的引脚,但它也允许您重复使用未使用的引脚。每当一个图钉从地图上消失(四处滚动等)时,该图钉不会被破坏,但需要在另一个图钉中重复使用。这样可以节省处理器和内存。

在您的Coordinator 类中添加以下函数:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    // create a unique identifier for pin reuse
    let identifier = "Placemark"
    
    // see if there already is a created pin
    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
    
    if annotationView == nil {
        // there wasn't a pin, so we make a new one
        annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
        
        // this is where your title is allowed to be shown when tapping the pin
        annotationView?.canShowCallout = true
        
        // this gives you an information button in the callout if needed
        // if you use the rightCalloutAccessoryView you must implement:
        // func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl)
        annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
    } else {
        // we have an old annotation, so update it
        annotationView?.annotation = annotation
    }
    
    return annotationView
}

【讨论】:

  • 我注意到它也将“我的位置”变成了一个图钉,我可以更改它,以便它仅显示我在地图上的位置的点而不是图钉吗?
  • 没关系,我自己想通了。再次感谢!
猜你喜欢
  • 2020-07-30
  • 2021-04-04
  • 2020-07-14
  • 1970-01-01
  • 1970-01-01
  • 2022-01-09
  • 1970-01-01
  • 1970-01-01
  • 2011-08-17
相关资源
最近更新 更多