Swift Mapbox自定义用户位置注释(致命错误)

时间:2017-11-12 23:55:07

标签: swift mapbox

我想创建一个自定义用户定位引脚,它从Firebase获取photoURL并显示它。我尝试使用此Swift 3 - Mapbox - Customize User Location Annotation作为参考。但是,我在构建自定义类时遇到错误。我不确定我可以在MGLUserLocationAnnotationView中编辑多少但似乎继承UIView所以我认为应该没问题。另外,我应该从View Class或View Controller中的Firebase获取数据吗?

这是我的自定义类

class CustomUserLocationDisplayView: MGLUserLocationAnnotationView {

  var view: UIView!

  @IBOutlet weak var displayView: UIImageView!
  var imageView: UIImageView!

  required override init(reuseIdentifier: String?) {
    super.init(reuseIdentifier: reuseIdentifier)
    setup()
  }
  override init(frame: CGRect) {
    super.init(frame: frame)
  }
  required init?(coder aDecoder: NSCoder) {
    fatalError("Error")         <--------- I get this FATAL Error
  }

  func readFirebaseUserProfile(){
    let user = Auth.auth().currentUser
    let photoURL = user?.photoURL
      displayView.kf.setImage(with: photoURL)
  }

  func setup() {
    view = loadViewFromNib()
    displayView.layer.cornerRadius = 30
    addSubview(view)
  }

  func loadViewFromNib() -> UIView {
    let bundle = Bundle(for: type(of: self))
    let nib = UINib(nibName: "CustomUserLocationDisplayView", bundle: bundle)
    let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView

    return view
  }
}

这是我的注释功能

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

    }else{
        var userLocationAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "user") as? CustomUserLocationDisplayView

        if userLocationAnnotationView == nil {
          userLocationAnnotationView = CustomUserLocationDisplayView(reuseIdentifier: reuseIdentifer)

        }

        return userLocationAnnotationView
      }
  }

1 个答案:

答案 0 :(得分:0)

一个可能的问题:我看不到您拨打readFirebaseUserProfile的位置。我会仔细检查它是否被调用。

另外,你可以在if userLocationAnnotationView == nil {之后设一个断点,看看你是否到达了它?您也可以尝试以下方式:

 if annotation is MGLUserLocation && mapView.userLocation != nil {
    userLocationAnnotationView = CustomUserLocationDisplayView(reuseIdentifier: reuseIdentifer)

    return userLocationAnnotationView
 } else if annotation is MGLPointAnnotation {
    // Set annotations here
 }
 return nil