1

我不知道为什么不显示标记..

我正在使用 Mapbox 6.0.0 版

注释视图类

// Create a subclass of MGLUserLocationAnnotationView.
class CustomUserLocationAnnotationView: MGLAnnotationView {
    var imageView: UIImageView!
    
    init(user: User) {
        super.init(reuseIdentifier: user.userId)
        print(">:> identifier : \(user.userId)")
        
        self.imageView = UIImageView(frame: CGRect(x: -10, y: -10, width: 30 , height: 30))
        self.imageView.layer.cornerRadius = imageView.frame.height / 2
        self.imageView.clipsToBounds = true
        self.imageView.image = UIImage(named: user.userProfileThumbnail ?? "profile")
        self.addSubview(self.imageView)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
}

}

注释覆盖方法

func mapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView? {
    guard annotation is MGLPointAnnotation, let wrapTitle = annotation.title, let title = wrapTitle else {
        return nil
    }
    
    if let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: title) {
        print(">:> \(title)")
        return annotationView
    } else {
        let view = CustomUserLocationAnnotationView(user: self.allUser[title]!)
        self.annotationViews.append(view)
        return view
    }
}

添加注释

private func reloadAnnotationView() {
    var pointAnnotations = [MGLPointAnnotation]()
    
    for user in allUser {
        let point = MGLPointAnnotation()
        point.coordinate = user.value.userLocation
        point.title = user.value.userId
        pointAnnotations.append(point)
    }
    
    print(">:> add annotation size \(pointAnnotations.count)")
    
    self.mapView?.addAnnotations(pointAnnotations)
}

我该如何处理它的问题?看起来您的帖子主要是代码;请添加更多细节。

谢谢

4

0 回答 0