【问题标题】:What replaces MGLAnnotationView in Version 10 of Mapbox?Mapbox 第 10 版中的 MGLAnnotationView 用什么代替?
【发布时间】:2022-04-28 11:15:20
【问题描述】:

在我们的应用程序中,由于 MGLAnnotationViews 的灵活性,我们大量使用它。

我们可以使用 MGLAnnotationView 为地图上的注释设置动画。

例如,注释上的脉动效果对我们的应用程序来说至关重要。

在之前的版本 6 中,我们通过以下方式实现了脉动效果:

class CustomAnnotationView: MGLAnnotationView {
    let pointFeature: MGLPointFeature
    let reuseId: String
    let customPulse = CustomPulseLayer() // CAReplicatorLayer

    init(pointFeature: MGLPointFeature, showPulse: Bool) {
        self.pointFeature = pointFeature
        self.reuseId = self.identifier(forFeature: pointFeature)
        super.init(annotation: pointFeature, reuseIdentifier: self.reuseId)
        self.frame = CGRect(x: 0, y: 0, width: 24, height: 24)
        self.layer.cornerRadius = bounds.width / 2
        self.backgroundColor = .systemBlue
        self.addSubview(pointImageView)

        if showPulse {
            layer.addSublayer(customPulse)
            customPulse.start()
        }
    }

    private lazy var pointImageView: UIImageView = {
        let imageView = UIImageView(frame: self.frame)
        let image = self.imageBasedOnIdentifier(self.reuseId)
        imageView.image = image
        return imageView
    }()
}

有人知道如何在 Mapbox 版本 10 中完成同样的事情吗?

【问题讨论】:

标签: mapbox-ios mapbox-ios-maps


【解决方案1】:

从 Mapbox v10.4.3 开始,对我有用的解决方案是使用 View Annotations。您可以在自定义注释视图中添加自定义手势识别器、动画等。 https://docs.mapbox.com/ios/maps/guides/annotations/view-annotations/#create-a-view-annotation

示例 sn-p

let options = ViewAnnotationOptions(geometry: Point(CLLocationCoordinate2D()), allowOverlap: true, anchor: .center)
  let annotationView = CustomAnnotationView()
  annotationView.rx.tapGesture().when(.recognized).subscribe(onNext: { [weak self] _ in
    guard let self = self else { return }
    self.viewModel.input.tappedAnnotation()
  }).disposed(by: self.disposeBag)
  annotationView.didChangeDragState = { [weak self] dragState in
    guard let self = self else { return }
    switch dragState {
    case .dragging:
      self.handleDragging()
    case .ending:
      self.handleEndDragging()
    default:
      break
    }
  }
  try? self.mapView.viewAnnotations.add(annotationView, options: options)

class CustomAnnotationView: UIView {
        // Do your customizations here
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-19
    • 2016-12-06
    • 1970-01-01
    • 2020-01-11
    • 2022-08-23
    • 2019-02-13
    • 1970-01-01
    相关资源
    最近更新 更多