MKOverlay color change already added to mapview

I have some MKOverlays (actually MKPolygons) that load right after the map appears. I would like to change their color dynamically. The only way I can do this is to remove the overlay and then add it with a new color. Is there a better way to do this on an existing overlay?

I'm new to objective-c / xcode / ios ... so be careful :)

+6
source share
2 answers

It is important to remember that most MapKit has different objects (MKPolygon, MKCircle, MKShape) for storing data related to drawing a view (MKPolygonView, MKCircleView, MKOverlayView, etc.). In many cases, you want to get a link to a view object so that you can set the background color. i.e.

MKOverlayView *anOverlay; //You need to set this view to the object you are interested in anOverlay.backgroundColor = [UIColor redColor]; [anOverlay setNeedsDisplay]; 

If your object is MKPolygon, you must determine the MKPolygonView into which it is pulled, then set the fillColor property and redraw the object by calling setNeedsDisplay:

 MKPolygonView *theView; theView.fillColor = [UIColor redColor]; [theView setNeedsDisplay]; 
+4
source

Your mapView has a method to get the rendering object for this overlay. You can then use the renderer to change the color of your overlay.

 if let renderer = mapView.rendererForOverlay(overlay) as? MKPolygonRenderer { renderer.fillColor = UIColor.redColor() } 

Leave the optional tide in MKPolygonRenderer if you are not looking for MKPolygon overlay.

(I understand this is a rather old question, but I just stumbled upon it and found my solution 😊)

0
source

Source: https://habr.com/ru/post/890100/


All Articles