I have a map custom view that inherit from MKOverlayPathView. I need this custom view to display circle, line and text.
I already managed to draw circle and line using path drawing CGPathAddArc and CGPathAddLineToPoint functions.
However, I still need to add text.
I tried to add text using
[text drawAtPoint:centerPoint withFont:font];
but I got invalid context error.
any idea?
With
MKOverlayPathView, I think the easiest way to add text is to overridedrawMapRect:zoomScale:inContext:and put the path and text drawing there (and do nothing in or don’t implementcreatePath).But if you’re going to use
drawMapRectanyway, you might want to just switch to subclassing a plainMKOverlayViewinstead ofMKOverlayPathView.With an
MKOverlayView, override thedrawMapRect:zoomScale:inContext:method and draw the circle usingCGContextAddArc(orCGContextAddEllipseInRectorCGPathAddArc).You can draw the text using
drawAtPointin this method which will have the requiredcontext.For example:
In relation to a comment in another answer…
When the center coordinate or radius (or whatever) of the associated
MKOverlaychanges, you can make theMKOverlayView“move” by callingsetNeedsDisplayInMapRect:on it (instead of removing and adding the overlay again). (When using aMKOverlayPathView, you can callinvalidatePathinstead.)When calling
setNeedsDisplayInMapRect:, you can pass theboundingMapRectof the overlay for the map rect parameter.In the LocationReminders sample app from WWDC 2010, the overlay view uses KVO to observe changes to the associated
MKOverlayand makes itself move whenever it detects a change to the circle’s properties but you could monitor the changes in other ways and callsetNeedsDisplayInMapRect:explicitly from outside the overlay view.(In a comment on another answer I did mention using
MKOverlayPathViewand that is how the LocationReminders app implements a moving circle overlay view. But I should have mentioned how you can also useMKOverlayViewto draw a circle. Sorry about that.)