Why is my image not being drawn in MKOverlayRenderer.draw method?

I have the following test code. It’s supposed to show an image on a map view. The image is not showing. I get no errors. I wonder if I made a mistake where I use the context where I scale or translate or draw. Would anyone help me out with this?

import UIKit
import MapKit
import CoreText

class LabelOverlayRenderer: MKOverlayRenderer {
    override func draw(_ mapRect: MKMapRect, zoomScale: MKZoomScale, in context: CGContext) {
        let image = UIImage(named: "myImage.png")!
        let imageReference = image.cgImage!
        let theMapRect = overlay.boundingMapRect
        let theRect = rect(for: theMapRect)
        context.scaleBy(x: 1, y: -1)
        context.translateBy(x: 0, y: -theRect.size.height)
        context.draw(imageReference, in: theRect)

import UIKit
import MapKit

class LabelOverlay: NSObject, MKOverlay {
    var coordinate: CLLocationCoordinate2D
    var boundingMapRect: MKMapRect
    var string: String
    init(coordinate: CLLocationCoordinate2D, string: String) {
        self.coordinate = coordinate
        let mapPoint = MKMapPointForCoordinate(coordinate)
        let mapSize = MKMapSize(width: 50, height: 21)
        boundingMapRect = MKMapRect(origin: mapPoint, size: mapSize)
        self.string = string

Hi @brower! Have you tried referencing this mapkit tutorial for overlays ? The “Adding Your Own Information” section has code that displays an overlay on a map.


This topic was automatically closed after 166 days. New replies are no longer allowed.