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()
}
}