Ios 自定义mkoverlayrender drawMapRect函数不绘制多边形

Ios 自定义mkoverlayrender drawMapRect函数不绘制多边形,ios,swift,mkoverlay,Ios,Swift,Mkoverlay,我已经构建了一个自定义的mkoverlayrender,以便构建多边形,应用混合模式,然后将它们添加到地图视图中。在drawMapRect函数中,我使用CGPoints数组来构建多边形,并创建路径 但是,在运行时,我的地图视图上不会显示任何内容。我最好的猜测是我在drawMapRect函数中创建多边形的顺序。任何帮助或指导都将不胜感激,谢谢 override func drawMapRect(mapRect: MKMapRect, zoomScale: MKZoomScale, inContex

我已经构建了一个自定义的mkoverlayrender,以便构建多边形,应用混合模式,然后将它们添加到地图视图中。在drawMapRect函数中,我使用CGPoints数组来构建多边形,并创建路径

但是,在运行时,我的地图视图上不会显示任何内容。我最好的猜测是我在drawMapRect函数中创建多边形的顺序。任何帮助或指导都将不胜感激,谢谢

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, self.fillColor)
    CGContextSetStrokeColorWithColor(context, UIColor.whiteColor().CGColor)
    CGContextSetLineWidth(context, 1.0)

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

    CGContextClosePath(context)
    CGContextDrawPath(context, CGPathDrawingMode.FillStroke)
    CGContextRestoreGState(context)
}
一些意见:

  • 您正在获取
    ,它已经是
    MKMapPoint
    的数组(至少在
    MKPolygonRenderer
    中),并调用
    MKMapPointForCoordinate
    。如果要使用
    MKMapPointForCoordinate
    ,则需要传递
    坐标,而不是
    。也许您已经定义了自己的属性,但我可能会更改属性的名称以避免混淆

  • 您需要使用
    pointForMapPoint
    将地图点转换为屏幕点

  • 另外,您正在调用
    CGContextSetFillColorWithColor
    ,但正在传递它
    fillColor
    。但是假设您的类是
    MKPolygonRenderer
    的子类,
    fillColor
    UIColor
    ,而不是
    CGColor

  • 您似乎正在访问一些属性,
    points
    ,但是
    MKPolygonRenderer
    没有
    points
    属性,而是
    points()
    方法

尽管如此,我还是不清楚你的代码是如何编译的。我怀疑您不是从
MKPolygonRenderer
子类化,而是从
mkoverlayrender
子类化,然后自己实现了一系列属性?如果子类
MKPolygonRender
,则可以免费获得所有多边形行为,只需实现
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)
    }

}

Rob在Swift 3中的回答

class MyRenderer: MKPolylineRenderer {

    override func draw(_ mapRect: MKMapRect, zoomScale: MKZoomScale, in context: CGContext) {

        context.saveGState()
        context.setBlendMode(CGBlendMode.exclusion)

        let clear = UIColor.clear

        context.setFillColor(clear.cgColor)

        let green = UIColor.green

        context.setStrokeColor(green.cgColor)

        context.setLineWidth(10.0)

        if self.polyline.pointCount > 1 {

            context.beginPath()

            let point_ = self.point(for: self.polyline.points()[0])

            context.move(to: CGPoint(x: point_.x, y: point_.y))

            for element in 1 ..< self.polyline.pointCount {

                let point_ = self.point(for: polyline.points()[element])

                context.addLine(to: CGPoint(x: point_.x, y: point_.y))

            }

            context.closePath()

            context.drawPath(using: .fillStroke)

        }

        context.restoreGState()

    }


 }
class MyRenderer:MKPolylineRenderer{
重写func draw(mapRect:MKMapRect,zoomScale:MKZoomScale,in-context:CGContext){
context.saveGState()
context.setBlendMode(CGBlendMode.exclusion)
let clear=UIColor.clear
context.setFillColor(clear.cgColor)
让绿色=UIColor.green
context.setStrokeColor(green.cgColor)
context.setLineWidth(10.0)
如果self.polyline.pointCount>1{
context.beginPath()
让点=self.point(对于:self.polyline.points()[0])
移动(到:CGPoint(x:point.x,y:point.y))
对于1..
罗伯在Swift 4中的回答

类MyCustomMapRenderer:MKPolygonRenderer{
重写func draw(mapRect:MKMapRect,zoomScale:MKZoomScale,in-context:CGContext){
//super.drawMapRect(mapRect,zoomScale:zoomScale,inContext:context)
context.saveGState()
context.setBlendMode(CGBlendMode.exclusion)
context.setFillColor(fillColor!.cgColor)
context.setStrokeColor(UIColor.white.cgColor)
context.setLineWidth(1.0)
如果polygon.pointCount>1{
context.beginPath()
设point=self.point(用于:polygon.points()[0])
上下文。移动(到:点)
对于1中的i..
您是否确认已正确添加注释,并且它是一个
MKPolygon
?例如,将断点添加到
rendererForOverlay
中,并确保命中了该代码段。如果是这样,请将断点添加到
drawMapRect
,并确保调用该断点。有时,这就像忽略设置地图视图的代理一样简单。在担心这个密码之前,我们先确认一下,他们两个肯定都被打电话了。它将在进入
drawMapRect
之前点击
renderforoverlay的
return,这确实奏效了。谢谢@Rob!我相信问题在于我将MkoveryRenderer子类化,只是让事情变得更加困难。在切换到子类化MKPolygonRenderer时,一切都很好!接下来,我意识到我的许多属性和变量名有些矛盾或过于混乱。现在流程正常运行,我肯定会重命名和重构它们。再次感谢你的帮助!
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")
}
class MyRenderer: MKPolylineRenderer {

    override func draw(_ mapRect: MKMapRect, zoomScale: MKZoomScale, in context: CGContext) {

        context.saveGState()
        context.setBlendMode(CGBlendMode.exclusion)

        let clear = UIColor.clear

        context.setFillColor(clear.cgColor)

        let green = UIColor.green

        context.setStrokeColor(green.cgColor)

        context.setLineWidth(10.0)

        if self.polyline.pointCount > 1 {

            context.beginPath()

            let point_ = self.point(for: self.polyline.points()[0])

            context.move(to: CGPoint(x: point_.x, y: point_.y))

            for element in 1 ..< self.polyline.pointCount {

                let point_ = self.point(for: polyline.points()[element])

                context.addLine(to: CGPoint(x: point_.x, y: point_.y))

            }

            context.closePath()

            context.drawPath(using: .fillStroke)

        }

        context.restoreGState()

    }


 }
class MyCustomMapRenderer: MKPolygonRenderer {

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

        context.saveGState()
        context.setBlendMode(CGBlendMode.exclusion)
        context.setFillColor(fillColor!.cgColor)
        context.setStrokeColor(UIColor.white.cgColor)
        context.setLineWidth(1.0)

        if polygon.pointCount > 1 {
            context.beginPath()

            let point = self.point(for: polygon.points()[0])
            context.move(to: point)
            for i in 1 ..< polygon.pointCount {
                let point = self.point(for: polygon.points()[i])
                context.addLine(to: point)
            }

            context.closePath()
            context.drawPath(using: CGPathDrawingMode.fillStroke)
        }
        context.restoreGState()
    }

}