在 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 添加手势识别器?

解决方法

不是返回 MGLAnnotationImage,而是返回 MGLAnnotationView,它只是 UIView 的子类。您可以为其附加一个手势识别器。

为了简化事情,让我们继承 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 委托是我们刚刚编写的,所以让我们通过创建一个协议来定义它,任何类对象都可以遵守该协议,可能是 UIViewController,以处理此方法。任何符合此协议的视图控制器现在都可以处理这些点击事件。

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

然后,无论哪个类对象显示我们的地图,可能是 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")
    }
}

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