1

我需要显示具有向下图钉的自定义注释,如下所示:

标记

所以我继承并MGLAnnotationView设置:centerOffsetlayoutSubviews()

class ZoneMapAnnotationView: MGLAnnotationView {

    // ...
    // code for view initialization is omitted
    // ...

    override func layoutSubviews() {
        super.layoutSubviews()
        centerOffset = CGVector(dx: bounds.width / 2, dy: -bounds.height / 2)
    }
}

结果,我在左上角看到了当前可见地图区域中不存在的部分注释视图:

左上角的标记

这是我实例化视图的方式:

extension ZonesMapWrappedView: MGLMapViewDelegate {

    func mapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView? {

        guard let annotation = annotation as? ZoneMapAnnotation else {
            return nil
        }

        let reuseIdentifier = "ZoneMapAnnotation"
        let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier) as? ZoneMapAnnotationView
            ?? ZoneMapAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)

        annotationView.bubbleColor = annotation.bubbleColor
        annotationView.text = annotation.title ?? ""

        return annotationView
    }
}

reuseIdentifier如果我在没有为每个注释提供或使其唯一的情况下创建注释视图,则不会出现此问题。但这样做对我来说没有意义。因为我认为这reuseIdentifier应该识别注释外观类型。

完整代码

这是一个演示我遇到的问题的存储库。

4

0 回答 0