在 MGLAnnotationImage 中添加手势识别器?

Adding gesture recognizers in MGLAnnotationImage?

这是我的 imageFor Mapbox 函数:

func mapView(_ mapView: MGLMapView, imageFor annotation: MGLAnnotation) -> MGLAnnotationImage? {
        var reuseid = ""
        switch annotation.subtitle ?? "" {
        case "uno":
            reuseid = "uno"
        case "dos":
            reuseid = "dos"
        case "tres":
            reuseid = "tres"
        default:
            reuseid = "default"
        }
        var annotationImage = mapView.dequeueReusableAnnotationImage(withIdentifier: reuseid)
        
        if annotationImage == nil {
            guard let image = UIImage(named: reuseid) else { return nil }
            annotationImage = MGLAnnotationImage(image: image, reuseIdentifier: reuseid)
            let tapGesture = AnnotationTapGestureRecognizer(target: self, action: #selector(Coordinator.tappedAnnotation(sender:)))
            tapGesture.annotation = annotation as! MGLPointAnnotation
            annotationImage.addGestureRecognizer(tapGesture) //error on this line says 'Value of type 'MGLAnnotationImage?' has no member 'addGestureRecognizer''
        }
        return annotationImage
    }

class AnnotationTapGestureRecognizer: UITapGestureRecognizer {
    var annotation = MGLPointAnnotation()
}

如何才能将手势识别器添加到要返回的 annotationImage 中?

不是 return MGLAnnotationImage,而是 return MGLAnnotationView,它只是 UIView 的子 class。您可以将手势识别器附加到它。

为了简化事情,让我们子class MGLAnnotationView 并创建我们的自定义标记。这个标记没什么特别的,只是一张图像(50 x 50 点)当然可以用我们的自定义识别器点击。为此,我只是有效地将标记设为 UIButton。当用户点击此标记时,标记将调用委托的 didSelectMarker(point:) 方法。

class XAnnotationView: MGLAnnotationView {
    weak var delegate: XMapMarkerDelegate?
    var point: MGLPointAnnotation?

    required override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
        let rect = CGRect(x: 0, y: 0, width: 50, height: 50)
        let button = UIButton(frame: rect)
        let image = UIImage(named: "mapMarker")

        button.setImage(image, for: .normal)
        button.setImage(image, for: .selected)
        button.setImage(image, for: .highlighted)
        button.imageView?.contentMode = .scaleAspectFit
        button.addTarget(self, action: #selector(markerAction), for: .touchUpInside)
        frame = rect
        addSubview(button)
        isEnabled = false // disable it if we're adding our own gesture recognizer
    }

    required init?(coder: NSCoder) {
        return nil
    }

    @objc private func markerAction() {
        delegate?.didSelectMarker(point: point)
    }
}

XMapMarkerDelegate 委托是我们刚刚创建的东西,所以让我们通过创建一个协议来定义它,任何 class 对象都可以遵守,可能是 UIViewController,来处理这个方法.任何符合此协议的视图控制器现在都可以处理这些点击事件。

protocol XMapMarkerDelegate: AnyObject {
    func didSelectMarker(point: MGLPointAnnotation)
}

然后无论哪个 class 对象正在显示我们的地图,可能是 UIViewController,使其符合我们的自定义协议并处理触摸事件。该对象很可能与地图的委托对象相同,因此让我们将它们组合在一起以实现更整洁的组织:

extension SomeViewController: MGLMapViewDelegate, XMapMarkerDelegate {
    /* This is one of Mapbox's many delegate methods.
       This method is for adding annotation views to the map. */
    func mapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView? {
        if annotation is MGLPointAnnotation { // only handle point annotations
            if let reusable = dequeueReusableAnnotationView(withIdentifier: "marker") as? XAnnotationView { // find a reusable marker if one is available with the given identifier
                reusable.point = annotation // assign this marker the current point
                return reusable
            } else { // if no reusable marker found, create a new marker with the given identifier
                let new = XAnnotationView(reuseIdentifier: "marker")
                new.delegate = self // assign self as the delegate
                new.point = annotation // assign this marker the current point
                return new
            }
        } else {
            return nil
        }
    }
    
    /* This is our custom delegate method. */
    func didSelectMarker(point: MGLPointAnnotation) {
        print("did tap marker")
    }
}

您可以根据需要向标记添加自定义数据或对象来对其进行自定义。您可以创建注释视图的多个子class,每个具有不同的大小或图像,并根据注释点在地图上使用不同的子class。可能性几乎是无限的。