Constantly update MKOverlay's position without UI glitching

In my app, I have an MKMapView. In the MKMapView, I create a circle (MKCircle) overlay and position it to the centerCoordinate. I then use the function that comes with the map delegate "mapViewDidChangeVisibleRegion(_ mapView: MKMapView)" to automatically delete the overlay, and add a new one in the new center of the map.

I got some really glitchy results with this. Individual map tiles that the overlay was covering would go unloaded for a tiny amount of time, but there were so many that did that and they did it so often, that it looked really bad.

Also, if I pan the map back and forth really quickly with my finger, the overlay can sometimes get delayed and not follow the center of the screen immediately.


How can I prevent this glitching from happening?

My first thoughts when looking at this, is that the glitchyness is caused by rapidly removing then re-adding the overlay. I think there are a couple of ways around this. Viewing your code would help choose the best, but in any case you could try:


1. Debouncing the amount of calls to remove and redraw the overlay. Only redraw if the size changes by a tolerance amount.

2. Cache the overlay / shape renderer and reuse.

3. If caching, and for small incremental size changes, instead of removing and redrawing the overlay, adjust your rendering code to fit the new size and redraw using

draw(_ mapRect: MKMapRect, zoomScale: MKZoomScale, in context: CGContext)

This fits in the code that draws, likely a subclass of MKOverlayRenderer. You would also need to take into account in the new boundingMapRect for the new shape. If following this approach I would therefore set the boundingMapRect of the circle to be large enough to take into account the maximum drawing size of the shape.


Also, does the circle have to be an overlay? You could use the mapViews conversion between map space and screen space to determine what screen size the circle needs to be and set that to the size and position of a UIView drawn 'above' the map.

Constantly update MKOverlay's position without UI glitching
 
 
Q