xCode 13.1 -- MKMap mapView.addOverlay -- Compiler Error: Invalid library file

Hi, I'm trying to add overlays to my map and I can't seem to make it happen. I get an error message in the console only when I add mapView.addOverlay(overlay) in my makeUIView

What is weird is that I still make it to the Coordinator func rendererFor overLay, because I sent a print() which is read inside the if statement

I get this message running it on simulator

I get this running on iPhone, but with no overlay on my map

Thanks for any help


import SwiftUI
import MapKit

struct MKMap: UIViewRepresentable {
   
  @ObservedObject var vm: MapViewModel = MapViewModel.shared
  let mapView = MKMapView()

  func makeCoordinator() -> Coordinator {
    Coordinator(self)
  }
   
   
  // This funciton is called when we make the view
  func makeUIView(context: Context) -> MKMapView {
     
    mapView.delegate = context.coordinator
    mapView.userLocation.title = "user"
    mapView.showsUserLocation = true

    let point = CLLocationCoordinate2D(latitude: -73.68118286132812, longitude: 45.48589125320114)
    let overlay = MKCircle(center: point, radius: 30)

    mapView.addOverlay(overlay)
     
    let region = MKCoordinateRegion(center: vm.center, span: vm.span)

    mapView.setRegion(region, animated: true)
     
    return mapView
  }
   
  // Function called when the view is updated
  func updateUIView(_ uiView: MKMapView, context: Context) {

  }
}

class Coordinator: NSObject, MKMapViewDelegate, CLLocationManagerDelegate{
  var parent: MKMap
  init(_ parent: MKMap){
    self.parent = parent
     
  }
   
   
  func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
     
    if overlay is MKCircle{
      print("--->>> I'm inside the rendererFor overlay in MKCircle Statement <<<---")
      let renderer = MKCircleRenderer(overlay: overlay)
      renderer.lineWidth = 100
      return renderer
    }
    return MKOverlayRenderer()
  }
}

If you could show a complete code (including MapViewModel and the view showing MKMap), more readers would be involved solving your issue.


The message Compiler Error: Invalid library file seems to be so called a log noise and you may need to ignore them.

You can find many articles discussing the issue searching with "Compiler Error: Invalid library file".

You can file a bug report, but do not expect too much, this issue has being left for years.


Your implementation of UIViewRepresentable has some inappropriate parts, but the core reason you cannot find the overlay on map might be because you have not set colors of MKCircleRenderer.

struct MKMap: UIViewRepresentable {
    
    @ObservedObject var vm: MapViewModel = MapViewModel.shared
    //↓Do not instantiate a UIView in a property initializer
    //let mapView = MKMapView()
    
    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }
    
    
    // This funciton is called when we make the view
    func makeUIView(context: Context) -> MKMapView {
        let mapView = MKMapView() //<-

        mapView.delegate = context.coordinator
        mapView.userLocation.title = "user"
        mapView.showsUserLocation = true
        
        let point = CLLocationCoordinate2D(latitude: -73.68118286132812, longitude: 45.48589125320114)
        let overlay = MKCircle(center: point, radius: 30)
        
        mapView.addOverlay(overlay)
        
        let region = MKCoordinateRegion(center: vm.center, span: vm.span)
        
        mapView.setRegion(region, animated: true)
        
        return mapView
    }
    
    // Function called when the view is updated
    func updateUIView(_ mapView: MKMapView, context: Context) {
        //↓↓You need to prepare for updates...
        let region = MKCoordinateRegion(center: vm.center, span: vm.span)
        mapView.setRegion(region, animated: true)
        //...
    }
}

class Coordinator: NSObject, MKMapViewDelegate, CLLocationManagerDelegate{
    var parent: MKMap
    init(_ parent: MKMap){
        self.parent = parent
        
    }
    
    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
        
        if let circle = overlay as? MKCircle { //<-
            print("--->>> I'm inside the rendererFor overlay in MKCircle Statement <<<---")
            let renderer = MKCircleRenderer(circle: circle) //<-
            renderer.fillColor = .green //<-#
            renderer.lineWidth = 100 //<- Isn't it too wide?
            renderer.strokeColor = .red //<-#
            return renderer
        }
        return MKOverlayRenderer()
    }
}

Did you ever find a solution, I am seeing this in 13.2

I do have the same Problem. Seeing this when adding a MKPolygon overlay. 13.2.1

xCode 13.1 -- MKMap mapView.addOverlay -- Compiler Error: Invalid library file
 
 
Q