【问题标题】:Adding a title in front/below a custom annotation mapkit swift 4在自定义注释mapkit swift 4的前面/下面添加标题
【发布时间】:2018-11-17 13:58:12
【问题描述】:

在我的应用程序中,我有一个充满注释的 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”下看起来自定义注释。

【问题讨论】:

  • 很难说出您的实际问题是什么。您应该通过edit 明确您想要做什么、尝试了什么以及得到了什么结果,包括任何错误消息。

标签: swift mapkit mapkitannotation


【解决方案1】:

你可以像这样创建一个标签:

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 来更改标签相对于注释的位置。负数为左上,正数右下。

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 我这样做了,注释的名称没有显示在它下面。标注气泡会弹出名称,但这不是我想要的,因为我有一个单独的视图来显示所有数据。
    【解决方案3】:

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

    MapKit Delegate Method:(我用过MKMarkerAnnotationView,用ma​​rkerTintColor设置颜色)

    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 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)
    

    【讨论】:

      猜你喜欢
      • 2023-03-15
      • 2020-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-06
      • 1970-01-01
      相关资源
      最近更新 更多