The shared infrastructure used to draw overlays on the map surface.


class MKOverlayRenderer : NSObject


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.

The MapKit 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.


Creating 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) -> CGPoint

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

func mapPoint(for: CGPoint) -> MKMapPoint

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

func rect(for: MKMapRect) -> CGRect

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

func mapRect(for: CGRect) -> MKMapRect

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

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 setNeedsDisplay(MKMapRect)

Invalidates the specified portion of the overlay at all zoom scales

func setNeedsDisplay(MKMapRect, zoomScale: MKZoomScale)

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


typealias MKZoomScale

A scale factor being used in conjunction with a map.

func MKRoadWidthAtZoomScale(MKZoomScale) -> CGFloat

Returns the width (in screen points) of roads on a map at the specified zoom level.


Inherits From

Conforms To

See Also

Annotations and Overlays

class MKPlacemark

A user-friendly description of a location on the map.

protocol MKAnnotation

An interface for associating your content with a specific map location.

class MKAnnotationView

The visual representation of one of your annotation objects.

protocol MKOverlay

An interface for associating content with a specific map region.

MapKit Annotations

Use these annotation objects as-is in your maps.

MapKit Overlays

Use these overlays as-is in your maps.