添加注释

时间:2017-10-10 19:47:14

标签: swift mapkit ios11

我有一个MKMapView,我希望在长按手势上添加注释。添加注释后,我想选择注释视图。我假设的一个简单的要求。

问题是我正在使用带有自定义rightCalloutAccessoryView和自定义detailCalloutAccessoryView的新MKMarkerAnnotationView。它还没有很好的文档记录,但是WWDC 2017 Session 237表示当标题/副标题不止时会显示标注。

不幸的是,对我来说情况并非如此。当我以编程方式(并手动)选择注释时,我得到一个奇怪的双选状态,在那里我可以看到标注和标记:

selected state

这是注释视图代码:

import Foundation
import MapKit

@available(iOS 11.0, *)
protocol TemporaryUserAnnotationViewDelegate: class {
    func temporaryUserAnnotationViewDidTapOk(title: String?, userAnnotationView: TemporaryUserAnnotationView)
}

@available(iOS 11.0, *)
class TemporaryUserAnnotationView: MKMarkerAnnotationView {

    weak var delegate: TemporaryUserAnnotationViewDelegate?
    var textField: UITextField!

    init(annotation: TemporaryUserAnnotation) {
        super.init(annotation: annotation, reuseIdentifier: TemporaryUserAnnotationMarkerAnnotationView.reuseIdentifier)
        markerTintColor = .lbc_blue
        canShowCallout = true
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func configure(annotation: TemporaryUserAnnotation) {
        let detailView = UIView()
        detailView.translatesAutoresizingMaskIntoConstraints = false
        textField = UITextField()
        textField.translatesAutoresizingMaskIntoConstraints = false
        textField.placeholder = "Titre"
        detailView.addSubview(textField)
        detailView.heightAnchor.constraint(equalToConstant: 21).isActive = true
        detailView.widthAnchor.constraint(equalToConstant: 100).isActive = true
        textField.centerXAnchor.constraint(equalTo: detailView.centerXAnchor).isActive = true
        textField.centerYAnchor.constraint(equalTo: detailView.centerYAnchor).isActive = true
        textField.heightAnchor.constraint(equalToConstant: 21).isActive = true
        textField.widthAnchor.constraint(equalToConstant: 100).isActive = true
        detailCalloutAccessoryView = detailView

        let rightView = UIView(frame: CGRect(origin: .zero, size: CGSize(width: 50, height: 70)))
        rightView.backgroundColor = .lbc_blue
        let button = UIButton(frame: CGRect(origin: .zero, size: CGSize(width: 50, height: 40)))
        button.setTitle("OK", for: .normal)
        button.setTitleColor(.white, for: .normal)
        button.addTarget(self, action: #selector(tapRightCalloutAccessoryViewButton), for: .touchUpInside)
        rightView.addSubview(button)
        rightCalloutAccessoryView = rightView
    }

    @objc func tapRightCalloutAccessoryViewButton() {
        delegate?.temporaryUserAnnotationViewDidTapOk(title: textField.text, userAnnotationView: self)
    }
}

这是控制器代码:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    print(#function)
    if annotation is MKUserLocation { return nil }
    if #available(iOS 11.0, *) {
        switch annotation {
        case let temporarayUserAnnotation as TemporaryUserAnnotation:
            if let view = mapView.dequeue() as? TemporaryUserAnnotationView {
                view.annotation = annotation
                return view
            } else {
                let view = TemporaryUserAnnotationView(annotation: temporarayUserAnnotation)
                view.delegate = self
                view.configure(annotation: temporarayUserAnnotation)
                return view
            }
        default:
            return nil
        }
    } else {
        let identifier = "marker"
        if let view = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKPinAnnotationView {
            view.annotation = annotation
            return view
        } else {
            return MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
        }
    }
}

我只是这样添加注释:

func addUserAnnotation(coordinate: CLLocationCoordinate2D) {
    let annotation = TemporaryUserAnnotation(coordinate: coordinate)
    mapView.addAnnotation(annotation)
    mapView.selectAnnotation(annotation, animated: true)
}

我还试图实现mapView(_:didAdd:)方法,但是在标注前面的标记视图会导致结果更糟。

任何帮助都将非常感谢!谢谢!

2 个答案:

答案 0 :(得分:2)

我找到了以下解决方法:当您添加注释时,请等待一秒钟,然后以编程方式选择它:

func addUserAnnotation(coordinate: CLLocationCoordinate2D) {
    let annotation = TemporaryUserAnnotation(coordinate: coordinate)
    mapView.addAnnotation(annotation)
    DispatchQueue.main.asyncAfter(wallDeadline: .now() + .seconds(1)) {
        self.mapView.selectAnnotation(annotation, animated: true)
    }
}

我仍然认为这不是必要的,所以如果您有更好的解决方案,请随时在此发布!

答案 1 :(得分:2)

我刚刚遇到了类似的问题,因为我有a(href=site style={ color: '#e64e65 !important' }) !{site}我自己与UITapGestureRecognizer的选择手势识别器竞争。我在点击事件中创建并选择了注释,但因为两个手势识别器都在触发,并且他们最后一次触发,所以它立即取消选择。

我的解决方案是实现MKMapView并成为我的手势识别器的代表,并实现以下内容:

UIGestureRecognizerDelegate