Swift MKMapView Polygon Overlay glitching

IosSwiftFunctionMkmapviewOverlay

Ios Problem Overview


In rare occasions, the overlay on my map (small blue dot) gets a weird glare (big blue area on right) (as seen in picture). Sometimes zooming in or out will fix it, but not always. Can't find anything on why this would happen. Is it something to do with how it is rendered?

enter image description here

func drawLocations(_ loc: CLLocation)
    {
        let center = CLLocationCoordinate2D(latitude: loc.coordinate.latitude, longitude: loc.coordinate.longitude)
        let lat: CLLocationDegrees = center.latitude
        let long: CLLocationDegrees = center.longitude
        var points = [CLLocationCoordinate2DMake(lat,long),CLLocationCoordinate2DMake(lat,long),CLLocationCoordinate2DMake(lat,long),CLLocationCoordinate2DMake(lat,long)]
        let polygon = MKPolygon(coordinates: &points, count: points.count)
        DispatchQueue.main.async(execute: {
            self.mapView.add(polygon)
        })
    }
func mapView(_ mapView: MKMapView!, rendererFor overlay: MKOverlay!) -> MKOverlayRenderer!
    {
        if overlay is MKPolygon
        {
            let polygonView = MKPolygonRenderer(overlay: overlay)
            polygonView.lineWidth = 4
            polygonView.strokeColor = UIColor(red: 30/255.0, green: 12/255.0, blue: 242/255.0, alpha: 0.4)
            return polygonView
        }
        return nil
    }

Ios Solutions


Solution 1 - Ios

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.

Solution 2 - Ios

To remove this bug, only solution is switch to Google Maps because that bug is linked with iOS 10+. You can report this bug to Apple Bug Reporting: https://developer.apple.com/bug-reporting/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSteveView Question on Stackoverflow
Solution 1 - IosPranav KasettiView Answer on Stackoverflow
Solution 2 - IosDhruv Narayan SinghView Answer on Stackoverflow