1

I don't know why the marker is not displayed..

I am using Mapbox version 6.0.0

annotationView class

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

}

annotation override method

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

add annotation

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

How do I approach it problem? It looks like your post is mostly code; please add some more details.

thanks

Park
  • 401
  • 4
  • 10
  • Check if your `guard` statement isn’t returning nil all the time. Use a breakpoint to see if something has no value. – Magnas Aug 15 '20 at 06:56
  • Also, where are you calling `reloadAnnotationView`? – Magnas Aug 15 '20 at 13:59
  • @Magnas Sorry for replying too late. I checked the guard statement in all the code, but it doesn't matter. Also added the point of calling reloadAnnotationView in the post. (++ tag) – Park Aug 17 '20 at 05:26
  • @Magnas Sorry. There was an error in the image I was using. Using a different random image solved the problem. There was no problem with the logic. Thank you. – Park Aug 18 '20 at 12:33

0 Answers0