Swift 在MGLAnnotationImage中添加手势识别器?

Swift 在MGLAnnotationImage中添加手势识别器?,swift,mapbox,uigesturerecognizer,gesture,uitapgesturerecognizer,Swift,Mapbox,Uigesturerecognizer,Gesture,Uitapgesturerecognizer,下面是我的imageFor Mapbox函数: func mapView(_ mapView: MGLMapView, imageFor annotation: MGLAnnotation) -> MGLAnnotationImage? { var reuseid = "" switch annotation.subtitle ?? "" { case "uno":

下面是我的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()
}

如何使其能够向返回的注释图像中添加手势识别器?

返回的不是
MGLAnnotationImage
,而是
MGLAnnotationView
,它只是
UIView
的一个子类。你可以在上面附加一个手势识别器

为了简化事情,让我们将
MGLAnnotationView
子类化,并创建自定义标记。这个标记没有什么特别之处,只是一个图像(50 x 50点),当然可以使用我们的自定义识别器进行点击。为了做到这一点,我将标记有效地设置为一个
ui按钮
。当用户点击此标记时,该标记将调用代理上的
didSelectMarker(点:)
方法

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

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

我想我可能必须返回MGLAnnotationImage,因为imageFor函数内置于Mapbox的协调器委托协议中。您是否有一个在过去为您工作过的特定实现?如果您愿意,我可以发布一些代码,但是Mapbox有一个专门用于返回
MGLAnnotationView
的委托方法,因此您可以使用该方法来代替当前使用的方法。他们的工作方式是一样的,一个只是处理图像和其他视图。@ NICCODENG2用代码做了一些编辑。MAPBOX推荐自定义地图数据的自定义标记,我们使用自定义注释视图,但是您可以从Mapbox下载它们并将它们添加到资产目录中:如果您不擅长创建自定义资产,则考虑免费寻找。PNG地图标记在线。