MapKit Rendering MKPolygon from geoJson stopped working

I have an app that has a large geoJson file that gets loaded and parsed. The code works in an older build in the AppStore, Xcode 12, but the polygons no longer renders in Xcode 13.2.

I isolated the code to a simple one page project and in there the polygon will render sometimes but it is very flakey, extremely slow and sometimes only partially renders. It also seems to cause the map to lock up. I have tested this on the simulator and a real device with the same results.

Anyone having the same issue? Is there a limit the number of point the polygon can have? The geoJson file has over 100,00 points.

Here is the code in simplified form:

let overlays = loadGeoJson()
mapView.addOverlays(overlays)

func loadGeoJson() -> [MKOverlay] {

        var overlays: [MKOverlay] = []

        var data: Data = Data()

        

        if let path = Bundle.main.path(forResource: fileName, ofType: nil) {

            do {

               data = try Data(contentsOf: URL(fileURLWithPath: path), options: NSData.ReadingOptions.mappedIfSafe)

            } catch {

                 return overlays

            }

            

            

            let decoder = MKGeoJSONDecoder()

            do {

                let objects = try decoder.decode(data)

                for object in objects {

                    if let feature = object as? MKGeoJSONFeature {

                        if let polygon = feature.geometry.first as? MKPolygon {

                                polygon.title = "Title of Polygon"

                                overlays.append(polygon)

                        }

                    }

                }

            } catch (let error) {

                return overlays

            }

        }
        return overlays

    }


extension MapViewController: MKMapViewDelegate {

    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {

        if let polygon = overlay as? MKPolygon {

            let renderer = MKPolygonRenderer(polygon: polygon)

            renderer.fillColor = .red

            renderer.strokeColor = .black

            renderer.lineWidth = 1

            renderer.alpha = 1.0

            return renderer

        }

        else if let multiPolygon = overlay as? MKMultiPolygon {

            let renderer = MKMultiPolygonRenderer(multiPolygon: multiPolygon)

            renderer.fillColor = .red

            renderer.strokeColor = .black

            renderer.lineWidth = 1

            renderer.alpha = 1.0

            return renderer

        }

        return MKOverlayRenderer()

    }

}

Replies

Is it possible that there could be errors in the GeoJSON file?
It may be hard to know this... ...but I wonder if some parsers are more "forgiving" than others, so perhaps there has been a change here.

If a polygon partially renders, then the map locks up, it sounds possible that the code is encountering an error in the polygon.

Does your file only contain one polygon, and do you only have the issue with this particular polygon?