在 front/below 自定义注释 mapkit swift 4 中添加标题

Adding a title in front/below a custom annotation mapkit swift 4

在我的应用程序中,我有一个充满注释的 mapkit,当单击一个时,我想要一个新视图向上滑动,其中包含注释的详细信息。如果我错了请纠正我,但我认为您需要创建自定义注释才能实现 didSelect() 方法。

问题是,默认情况下,弹出的自定义注释没有注释的名称,就像默认的 mapkit 注释一样,而且由于我一次有大约 20 个注释,用户无法知道他们选择了什么。

知道如何使用注释名称在自定义注释下方添加标题或标签吗?我不想在注释上方弹出一个调用,因为我正在向上滑动视图,其中填充了注释数据。

这是我的:

extension ViewController : MKMapViewDelegate {

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

    if annotation is MKUserLocation { return nil }



    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "pin") as? MKPinAnnotationView

    if annotationView == nil {

        annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "pin")

        annotationView?.animatesDrop = true

        annotationView?.canShowCallout = false

    } else {

        annotationView?.annotation = annotation

    }



    return annotationView

}



func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {

    UIView.animate(withDuration: 0.2) {
        // the view that will hold the annotations data

        self.annotationInfoViewBottomConstraint.constant = 0

        self.view.layoutIfNeeded()

    }

}

}

如您所见,默认注释在下面有位置的名称,这是我想要的,但我想要它在 "pin" 下寻找自定义注释。

您需要将 annotationView.enabled 和 annotationView.canShowCallout 设置为 YES

您可以像这样创建标签:

let annotationLabel = UILabel(frame: CGRect(x: -40, y: -35, width: 105, height: 30))
annotationLabel.numberOfLines = 3
annotationLabel.textAlignment = .center
annotationLabel.font = UIFont(name: "Rockwell", size: 10)
// you can customize it anyway you want like any other label

设置文本:

annotationLabel.text = annotation.title!!

然后添加到注释视图:

annotationView.addSubview(annotationLabel)

Picture of annotation with label

我还添加了背景和边框:

annotationLabel.backgroundColor = UIColor.white
annotationLabel.layer.cornerRadius = 15
annotationLabel.clipsToBounds = true

您还可以通过在创建标签时更改 X 和 Y 来更改标签相对于注释的位置。负向左上,正向右下

标记下的名称和颜色变化的简单解决方案:

MapKit 委托方法:(我使用了MKMarkerAnnotationView 并使用markerTintColor 设置颜色)

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "pin") as? MKMarkerAnnotationView
        
        guard let annotation = annotation as? PlaceAnnotation else {
            return annotationView
        }
        
        
        if annotationView == nil {
            annotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: "pin")
        } else {
            annotationView?.annotation = annotation
        }
        
        annotationView?.markerTintColor = annotation.pinTintColor
        
        return annotationView
    }

MKAnnotation 自定义 Class:

class PlaceAnnotation: NSObject, MKAnnotation  {
    let title: String?
    let locationName: String
    let discipline: String
    let coordinate: CLLocationCoordinate2D
    var pinTintColor: UIColor
    
init(id: String, title: String, locationName: String, discipline: String, coordinate: CLLocationCoordinate2D, pinTintColor: UIColor) {
    self.title = title
    self.locationName = locationName
    self.discipline = discipline
    self.coordinate = coordinate
    self.pinTintColor = pinTintColor
    
    super.init()
    }
}

如何使用:

let place = PlaceAnnotation(title: "My Location", locationName: "", discipline: "", coordinate: CLLocationCoordinate2D(latitude: lat, longitude: lng), pinTintColor: UIColor.systemBlue)
map.addAnnotation(place)