1

I am working with MGLPolygon to have an overlay on mapView with Mapbox SDK.

Below is my current code:

- (UIColor *)mapView:(MGLMapView *)mapView fillColorForPolygonAnnotation:(MGLPolygon *)annotation
{
    return [UIColor colorWithWhite:1.0 alpha:0.5];
}

But the MapBox only supply the delegate to fill the color. The MapKit can redefine the MKPolygonRederer. Below is an example. (from Custom MKOverlayRenderer drawMapRect function not drawing polygons):

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)
    }

}

Please give me some clue. Thank you in advance.

Zgpeace
  • 3,927
  • 33
  • 31

0 Answers0