Custom Mkoverlayrenderer Drawmaprect Function Not Drawing Polygons

Custom MKOverlayRenderer drawMapRect function not drawing polygons

A few observations:

  • You are taking points, which is already an array of MKMapPoint (in MKPolygonRenderer, at least) and calling MKMapPointForCoordinate. If you want to use MKMapPointForCoordinate, you'd pass it coordinates, not points. Maybe you've defined your own properties, but I might change the name of the property to avoid confusion.

  • You need to convert map points to screen points with pointForMapPoint.

  • Also, you are calling CGContextSetFillColorWithColor, but passing it fillColor. But assuming your class is a subclass of MKPolygonRenderer, the fillColor is a UIColor, not a CGColor.

  • You seem to be accessing some property, points, but the MKPolygonRenderer doesn't have points property, but rather points() method.

With all of that said, I'm unclear how your code even compiled. I suspect you didn't subclass from MKPolygonRenderer, but rather subclassed MKOverlayRenderer and then implemented a bunch of properties yourself? If you subclass MKPolygonRender, you get all the polygon behavior for free and only have to implement drawMapRect:

class MyCustomMapRenderer: MKPolygonRenderer {

override func drawMapRect(mapRect: MKMapRect, zoomScale: MKZoomScale, inContext context: CGContext) {
//super.drawMapRect(mapRect, zoomScale: zoomScale, inContext: context)

CGContextSaveGState(context)
CGContextSetBlendMode(context, CGBlendMode.Exclusion)
CGContextSetFillColorWithColor(context, fillColor!.CGColor)
CGContextSetStrokeColorWithColor(context, UIColor.whiteColor().CGColor)
CGContextSetLineWidth(context, 1.0)

if polygon.pointCount > 1 {
CGContextBeginPath(context)

let point = pointForMapPoint(polygon.points()[0])
CGContextMoveToPoint(context, CGFloat(point.x), CGFloat(point.y))
for i in 1 ..< polygon.pointCount {
let point = pointForMapPoint(polygon.points()[i])
CGContextAddLineToPoint(context, CGFloat(point.x), CGFloat(point.y))
}

CGContextClosePath(context)
CGContextDrawPath(context, CGPathDrawingMode.FillStroke)
}
CGContextRestoreGState(context)
}

}

By the way, we probably could be more sophisticated here (checking to see if the points were visible, figuring out what points to render given the scale ... i.e. if polygon with thousands of points and being scaled into a 100x100 portion of the view, perhaps you don't have to render all of the points, etc.). See WWDC 2010 Customizing Maps with Overlays, which, while dated, is still relevant.

As an aside, your rendererForOverlay is curious, too. You are calling some custom initialization method (which is fine), but you're passing overlay and currentPolygon. But the overlay is the polygon, so I don't know what this currentPolygon is. And rendererForOverlay is stateless, so I'd discourage you from referencing some property, but rather just take the overlay that was passed to the method. That way, you could have multiple polygons and let map view keep track of which is which. So I'd do something like:

func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer {
if let polygon = overlay as? MKPolygon {
let polygonRenderer = MyCustomMapRenderer(polygon: polygon)
polygonRenderer.fillColor = polyFactory!.getPolygonColor()
return polygonRenderer
}
fatalError("Unexpected overlay type")
}

MKOverlayRenderer function not being called?

It’s because your overlay is not visible because the latitude and longitude are backwards. Swap them and you’ll start to see your overlays:

let coordinate = CLLocationCoordinate2DMake(self.coordinaten[x][y][1], self.coordinaten[x][y][0])

Overridden MKMapView drawMapRect method returning errors

1.

you put the code into your ViewController -- the view controller isn't a map view though :)

Your VC may HAVE a map view but it doesn't ISNT a mapview

2.

BUT EVEN MORE CRITICAL:

the code doesn't belong in a mapView either. you try to use a map overlay. look into using MKOverlayRenderer


Thats what both error messages say basically:

  1. says your SELF(ViewController) doesn't have a method named rectForMapRect
  2. says your SUPER(UIViewController) doesn't have a method named drawMapRect

==> only a MKOverlayRenderer provides them (that is visible from the docs if you search for the two methods)



Related Topics



Leave a reply



Submit