Hi I'm trying to create an overlay around my annotations, like apples reminders app, I have already created an MKCircle object which I think I should use to show the overlay but how do I convert my MKCircle object into an MKOVerlay object? maybe there's a better way to add annotations? I'm new to swift and programming. Any suggestions?
Asked
Active
Viewed 992 times
2
-
oh I got some error before but it seems to work now, but how do I get the MKCircle to show around the annotation? mapView.add(overlay) don't seem to work – CFRJ Jul 05 '17 at 20:22
1 Answers
4
MKCircle
is a MKOverlay
object. You just need to add it as an overlay:
let circle = MKCircle(center: coordinate, radius: 1000)
mapView.add(circle)
Of course, you have to tell the map how to render it by implementing mapView(_:rendererFor:)
in your delegate and instantiate a MKCircleRenderer
for the MKCircle
that’s passed as an overlay.
extension ViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKCircleRenderer(overlay: overlay)
renderer.fillColor = UIColor.cyan.withAlphaComponent(0.5)
renderer.strokeColor = UIColor.cyan.withAlphaComponent(0.8)
return renderer
}
}
Clearly, make sure you specified the delegate
for your MKMapView
, too. And if you have other types of renderers, you might implement specific logic for those, too, e.g.
extension ViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
if let circle = overlay as? MKCircle {
let renderer = MKCircleRenderer(circle: circle)
renderer.fillColor = UIColor.cyan.withAlphaComponent(0.5)
renderer.strokeColor = UIColor.cyan.withAlphaComponent(0.8)
return renderer
}
if let polygon = overlay as? MKPolygon {
let renderer = MKPolygonRenderer(polygon: polygon)
renderer.fillColor = UIColor.blue.withAlphaComponent(0.5)
renderer.strokeColor = UIColor.blue.withAlphaComponent(0.8)
return renderer
}
if let polyline = overlay as? MKPolyline {
let renderer = MKPolylineRenderer(polyline: polyline)
renderer.fillColor = UIColor.red.withAlphaComponent(0.5)
renderer.strokeColor = UIColor.red.withAlphaComponent(0.8)
return renderer
}
fatalError("Unexpected overlay type")
}
}

Rob
- 415,655
- 72
- 787
- 1,044
-
Hi so I added the first code to my program but it don't seem to work. When is the renderFor function called? I found from the documentation it's called when "The renderer object is responsible for drawing the contents of your overlay when asked to do so by the map view. " How do I ask the render object to draw? Sorry if I ask stupid questions – CFRJ Jul 06 '17 at 07:22
-
It's called for you automatically if the overlay overlaps with the visible portion of the map. The only trick is to make sure to set the `delegate` of the map view. You can either do this in IB, or in `viewDidLoad` with `mapView.delegate = self`. – Rob Jul 06 '17 at 07:52
-
hey any ideas if I want to add a handle on the overlay object to be able to resize the radius? – CFRJ Jul 06 '17 at 08:45