'Error when adding polygon overlay to MKMapView

I have a map view that is conforming to UIViewRepresentable to use it in my SwiftUI app. When my view model updates with data after a successful API call, I add to my array of overlays with an array of coordinates to represent a map boundary. This array of overlays is then passed to the view and then the overlay is added as an MKPolygon in updateUIView. I return a render this polygon in the delegate method.

struct MapViewUIKit: UIViewRepresentable {
    @Binding var annotations: [MKPointAnnotation]
    @Binding var mapRect: MKMapRect
    @Binding var overlays: [MKOverlay]
    let mapView = MKMapView()
    
    func makeUIView(context: Context) -> MKMapView {
        mapView.delegate = context.coordinator
        return mapView
    }
    
    func updateUIView(_ view: MKMapView, context: Context) {
        view.setVisibleMapRect(mapRect, animated: true)
        
        if annotations.count != view.annotations.count {
            view.removeAnnotations(view.annotations)
            view.addAnnotations(annotations)
        }
        
        if overlays.count != view.overlays.count {
            view.removeOverlays(view.overlays)
            view.addOverlays(overlays)
        }
    }
    
    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }
    
    class Coordinator: NSObject, MKMapViewDelegate {
        var parent: MapViewUIKit
        
        init(_ parent: MapViewUIKit) {
            self.parent = parent
        }
        
        func mapViewDidChangeVisibleRegion(_ mapView: MKMapView) {
            parent.mapRect = mapView.visibleMapRect
        }
        
        func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
            if overlay is MKPolygon {
                let renderer = MKPolygonRenderer(overlay: overlay)
                renderer.strokeColor = .magenta
                renderer.fillColor = .blue
                return renderer
            }
            return MKOverlayRenderer()
        }
    }
}

However, when I zoom into the area where the polygon should be rendered, I get the following error and there is no overlay

Wrapped around the polygon without finishing... :-(
List has 6 nodes:
    1 2 4 5 6 0 
2022-03-14 15:01:36.159966-0400 Synop[3702:59086] [VKDefault] Building failed to triangulate!

I've tried searching what this error means to no avail. I know my overlay is being added and with the proper coordinates as well with print statements in the relevant places.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source