Class

MKOverlayRenderer

The MKOverlayRenderer class defines the basic behavior associated with all map-based overlays. An overlay renderer draws the visual representation of an overlay object—that is, an object that conforms to the MKOverlay protocol. This class defines the drawing infrastructure used by the map view. Subclasses are expected to override the draw(_:zoomScale:in:) method to draw the contents of the overlay.

Overview

The Map Kit framework provides several concrete instances of overlay renderers. Specifically, it provides renderers for each of the concrete overlay objects. You can use one of these existing renderers or define your own subclasses if you want to draw the overlay contents differently.

Subclassing Notes

You can subclass MKOverlayRenderer to create overlays based on custom shapes, content, or drawing techniques. The only method subclasses are expected to override is the draw(_:zoomScale:in:) method. However, if your class contains content that may not be ready for drawing right away, you should also override the canDraw(_:zoomScale:) method and use it to report when your class is ready and able to draw.

The map view may tile large overlays and distribute the rendering of each tile to separate threads. Therefore, the implementation of your draw(_:zoomScale:in:) method must be safe to run from background threads and from multiple threads simultaneously.

Symbols

Initializing an Overlay View

init(overlay: MKOverlay)

Initializes and returns the overlay renderer and associates it with the specified overlay object.

Attributes of the Overlay

var overlay: MKOverlay

The overlay object containing the data for drawing.

var alpha: CGFloat

The amount of transparency to apply to the overlay.

var contentScaleFactor: CGFloat

The scale factor used to draw the overlay’s content.

Converting Points on the Map

func point(for: MKMapPoint)

Returns the point in the overlay renderer’s drawing area corresponding to the specified point on the map.

func mapPoint(for: CGPoint)

Returns the point on the map that corresponds to the specified point in the overlay renderer’s drawing area.

func rect(for: MKMapRect)

Returns the rectangle in the overlay renderer’s drawing area corresponding to the specified rectangle on the map.

func mapRect(for: CGRect)

Returns the rectangle on the map that corresponds to the specified rectangle in the overlay renderer’s drawing area.

Drawing the Overlay

func canDraw(MKMapRect, zoomScale: MKZoomScale)

Returns a Boolean value indicating whether the overlay view is ready to draw its content.

func draw(MKMapRect, zoomScale: MKZoomScale, in: CGContext)

Draws the overlay’s contents at the specified location on the map.

func setNeedsDisplay()

Invalidates the entire contents of the overlay for all zoom scales.

func setNeedsDisplayIn(MKMapRect)

Invalidates the specified portion of the overlay at all zoom scales

func setNeedsDisplayIn(MKMapRect, zoomScale: MKZoomScale)

Invalidates the specified portion of the overlay but only at the specified zoom scale.

Relationships

Inherits From

Conforms To