Swift MKMapView Polygon Overlay glitching

It looks like you’re using an MKPolygon even though you’re only drawing a single point, with the same latitude and longitude. I think it would be better to use an MKCircle for a single point.

func drawLocation(_ loc: CLLocation)
    {
        let center = CLLocationCoordinate2D(latitude: loc.coordinate.latitude, longitude: loc.coordinate.longitude)
        let circle = MKCircle(center: center, radius: 4)
        DispatchQueue.main.async(execute: {
            self.mapView.add(circle)
        })
    }
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer
    {
        if overlay is MKCircle
        {
            let circleView = MKCircleRenderer(circle: overlay)
            circleView.lineWidth = 4
            circleView.strokeColor = UIColor(red: 30/255.0, green: 12/255.0, blue: 242/255.0, alpha: 0.4)
            return circleView
        }
        return nil
    }

As you say, the zooming feature in and out is what could cause this glitch because the points when you zoom in may look more like a polygon, and when you zoom out the renderer still has polygon artefacts but is now rendering only a single coordinate.

Usually it’s better to use a polygon to render points which are spaced further apart, this helps the renderer since the map renders on a tile-by-tile basis which makes it easy to distinguish points in the polygon. For multiple points which are very close together then we can consider grouping them to use a larger radius MKCircle. You can also consider setting isZoomEnabled to false for the MKMapView and setting a fixed MKCoordinateRegion at the desired zoom level to avoid these rendering artefacts.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)