0

So I can create annotations that I stick on the map, and I can create overlays, say either polygons or polylines, and have those drawn on the map with default renderers. Everything works as expected.

And I can even create an annotation view with a custom image that is drawn with an orientation. The problem is, I can't get a notification as the map is rotating, but only when it is done rotating, and I'd like to change my annotation view as the map is rotating (because my annotation involves orientation).

So I thought, hey, the polygons and lines get reoriented as the map is rotated, so I'll change my annotation to an overlay. The problem is, I can't get my custom renderer to draw anything.

To test, I created a subclass of MKPolylineRenderer and overrode the draw method. And looking at a coordinate of the line:

self.polyline.points()[0]

It looks fine I guess, e.g.,

MKMapPoint(x: 263670550.06153709, y: 169200197.90301803)

But every time I try to convert that map point to a CGPoint, the y coordinate is always 0, and the x coordinate seems way too big:

let point = self.point(for: self.polyline.points()[0])
-> (52506.4039975107, 0.0)

If I call my superclass's draw method I can see my polyline, and the first point is definitely not at y=0, but I can't get anything in my overridden method to show up (again, seemingly because of registration issues, but not sure). I followed this advice:

Custom MKOverlayRenderer drawMapRect function not drawing polygons

Any ideas?

rjcarr
  • 2,072
  • 2
  • 22
  • 35

1 Answers1

0

In the MKMapViewDelegate method, you have a function that can certainly help you. In my app, I've managed it this way.

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    let overlayRenderer = MKCircleRenderer(circle: overlay as! MKCircle)
    overlayRenderer.fillColor = UIColor.green
    overlayRenderer.alpha = 0.2
    return overlayRenderer
}

And in the function that is used to locate my user:

//Overlays
let circleDraw = MKCircle(center: coordinates, radius: 
UI_mapView.add(circleDraw)

Hope that helps.

schtipoun
  • 423
  • 3
  • 9
  • Thanks, as I said the renderer is already rendering the polyline, I just can't get it to render anything custom. I can see that it is actually calling my custom renderer method, and then I call the super method, but nothing I draw is actually showing up. Cheers. – rjcarr Nov 08 '17 at 22:38
  • Oh I think I replied too quickly! Sorry for that ;) – schtipoun Nov 08 '17 at 22:53