LocationMessageDisplayDelegate to add custom annotations#150
Conversation
…ationMessages Modified example proyect implementing LocationMessageDisplayDelegate with a custom annotation
|
|
||
| open weak var messageCellDelegate: MessageCellDelegate? | ||
|
|
||
| open weak var locationMessagesDisplayDelegate: LocationMessageDisplayDelegate? |
There was a problem hiding this comment.
We don't need this property because we can assign it to messagesDisplayDelegate since LocationMessageDisplayDelegate conforms to MessagesDisplayDelegate
|
|
||
| guard let snapshot = snapshot, error == nil else { | ||
| //show an error image? | ||
| return |
There was a problem hiding this comment.
We could use defer {} to never forget again 😬
…ectionView
Added defer {} to always exit `setMapSnaphotImage` stopping activity indicator
| point.x += annotationView.centerOffset.x | ||
| point.y += annotationView.centerOffset.y | ||
|
|
||
| annotationView.image?.draw(at: point) |
There was a problem hiding this comment.
My only question is what if the user is not using an image for the MKAnnotationView? It seems like we're not using MKAnnotationView at all really. It's just wrapping up our UIImage. Am I making sense?
There was a problem hiding this comment.
MKAnnotationView is not only wrapping the image but also the anchor information necessary for drawing annotationView.centerOffset, so we shouldn't just ask for an image in the delegate.
But is true that this API how it is now, allows the mistake of not adding an image, so maybe we could use a simpler wrapping Struct with image and centerOffset making the image required.
I also thought some users might not want so show an annotation at all, so we could make the delegate return optional adding the possibility of a map without annotation.
There was a problem hiding this comment.
@etoledom Isn't there some way to add the MKAnnotationView as a whole?
There was a problem hiding this comment.
What I'm trying to say is what if I want to style my MKAnnotationView using the properties of UIView and not just a UIImage
There was a problem hiding this comment.
@SD10 I see what you mean.
We can actually draw the annotationView's layer with:
annotationView.layer.render(in: context)
I tested it and seems to work fine.
For some reason, every tutorial and example I have found uses the image drawing thing, but the Apple documentation on MKAnnotationView says that is allowed also to draw the pin overriding draw(_:), so in that case probably will be the best, as you said, to draw the whole view instead of just the image for maximum flexibility.
I will test it a bit more, and PR with this little drawing change.
There was a problem hiding this comment.
Works with MKAnnotationView, MKMarkerAnnotationView, custom subclasses of MKAnnotationView, but it doesn't want to work with MKPinAnnotationView (our default) 😅
Documentation on LocationMessageDisplayDelegate
| self.activityIndicator.stopAnimating() | ||
| } | ||
| guard let snapshot = snapshot, error == nil else { | ||
| print("\(#function) Error creating map snapshot: \(String(describing: error))") //show an error image? |
There was a problem hiding this comment.
We don't want to print anything in production. Let's just return here
There was a problem hiding this comment.
Sure!, I'll fix it now
|
@etoledom Thanks for your great work on this 💯 👍 I've invited you to the MessageKit organization, it would be great to have your involvement in our project 😃 No worries if you're not feeling up to it! |
Added LocationMessageDisplayDelegate to add custom annotations to locationMessages
Modified example proyect implementing LocationMessageDisplayDelegate with a custom annotation