Custom MKOverlayRenderer drawMapRect function not drawing polygons
A few observations:
You are taking
points
, which is already an array ofMKMapPoint
(inMKPolygonRenderer
, at least) and callingMKMapPointForCoordinate
. If you want to useMKMapPointForCoordinate
, you'd pass itcoordinates
, notpoints
. 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 itfillColor
. But assuming your class is a subclass ofMKPolygonRenderer
, thefillColor
is aUIColor
, not aCGColor
.You seem to be accessing some property,
points
, but theMKPolygonRenderer
doesn't havepoints
property, but ratherpoints()
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:
- says your SELF(ViewController) doesn't have a method named
rectForMapRect
- 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
How to Deal with Dynamic Sections and Rows in iOS Uitableview
Iad Interstitials Not Showing Consistently? and Not at All on the Simulator
Uitableviewcell Custom Class - Reload Cell Height After Change in Subview Height Constraint
Random Number from an Array Without Repeating the Same Number Twice in a Row
Empty Return Value from Swift Function Containing Closure
Non-Main Bundle File as Alert Sound
Alamofire Multipart Upload Post Error in Swift
Why Is the Title Being Repeated
I Want to Switch Time Scrubber to Do 15 Min Intervals
Mapbox Navigation Pod Unable to Install with Objectivec Where I am Using Xcode 9.2
Video with Gpuimagechromakeyfilter Has Tint When Played in Transparent Gpuimageview
If Let Doesn't Unwrap Optional Value for Mkannotation's Title Property
How to Transfer Data Between Parent and Child View Controllers
Can't Set Calayer's Border Color
Change Duration (Speed) on a Running Animation
Uitableviewcell Shows Incorrect Results from Document Folder