Reputation: 79
I am trying to add a custom overlay onto my map view to create an inverted circle (rather than the typical MKCircle overlay around a pin on the map). I've tried following this thread: Add inverted circle overlay to map view
I have not created a custom MKOverlay subclass, but I have created a custom MKOverlayRenderer subclass that I'm trying to use with MKCircle as the MKOverlay.
class MKInvertedCircleRenderer: MKOverlayRenderer {
var fillColor: UIColor = UIColor.blue
override func draw(_ mapRect: MKMapRect, zoomScale: MKZoomScale, in context: CGContext) {
let path = UIBezierPath(rect: CGRect(x: mapRect.origin.x, y: mapRect.origin.y, width: mapRect.size.width, height: mapRect.size.height))
context.setFillColor(fillColor.cgColor)
context.addPath(path.cgPath)
context.fillPath()
}}
And this is my mapView delegate call in my view controller:
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
switch(overlaySegmentedControl.selectedSegmentIndex) {
case 0: // Draw circle on map view for region specified for "When I Arrive".
let circleRenderer = MKCircleRenderer(overlay: overlay)
circleRenderer.strokeColor = self.view.tintColor
circleRenderer.lineWidth = 3.0
circleRenderer.fillColor = self.view.tintColor.withAlphaComponent(0.15)
return circleRenderer
case 1: // Draw rectangle with circle cutout on map view for region specified for "When I Leave".
let circleRenderer = MKInvertedCircleRenderer(overlay: overlay)
return circleRenderer
default:
fatalError("Unspecified segmented control selected: \(overlaySegmentedControl.selectedSegmentIndex)")
}
}
Currently, case 0 works as expected, by outputting a circle overlay around a pin on the map view.
Case 1 does not work, I was expecting it to draw a rect on the map, filled with my fillColor set in the MKOverlayRenderer subclass - however nothing shows up. I am trying to troubleshoot this before adding the path to cut out a circle in the middle.
Any help would be appreciated!
Upvotes: 2
Views: 501
Reputation: 79
Turns out you can't use MKCircle with a custom MKOverlayRenderer subclass.
You have to create your own custom MKOverlay subclass to work with the custom MKOverlayRenderer subclass.
Upvotes: 0