Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift MKMapView Polygon Overlay glitching

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
    }
like image 341
Steve Avatar asked Nov 29 '16 18:11

Steve


1 Answers

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.

like image 101
Pranav Kasetti Avatar answered Nov 15 '22 23:11

Pranav Kasetti