いくつかの観察:あなたはすでにMKMapPoint
の配列であるpoints
を取っている
を(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, 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)
}
は私のカスタムオーバーレイレンダラーが初期化されているところであります、少なくとも)と呼び出しMKMapPointForCoordinate
。 MKMapPointForCoordinate
を使用する場合は、でなくcoordinates
にします。points
ではなく、coordinates
とします。自分のプロパティを定義している可能性もありますが、混乱を避けるためにプロパティの名前を変更する可能性があります。
pointForMapPoint
でマップポイントをスクリーンポイントに変換する必要があります。
また、CGContextSetFillColorWithColor
を呼び出していますが、fillColor
を渡しています。しかし、あなたのクラスがMKPolygonRenderer
のサブクラスであると仮定すると、fillColor
はであり、CGColor
ではありません。
あなたはpoints
、いくつかのプロパティにアクセスしているように見えるが、MKPolygonRenderer
はpoints
性質を持っているのではなく、points()
方法はありません。
これらのことはすべてあなたのコードがどのようにコンパイルされているかわかりません。私はあなたがMKPolygonRenderer
からサブクラス化されていないと思うが、むしろサブクラスMKOverlayRenderer
とし、あなたが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)
}
}
を、我々はおそらく考え出す、ポイントが見えていたかどうかをチェックする(ここでは、より洗練された可能性がありスケールが与えられてレンダリングするポイント...数千ポイントのポリゴンで、ビューの100x100の部分に拡大縮小されている場合は、すべてのポイントをレンダリングする必要はありません。 WWDC 2010 Customizing Maps with Overlaysを参照してください。
脇に、あなたのrendererForOverlay
も興味があります。カスタム初期化メソッド(これは問題ありません)を呼び出していますが、overlay
とcurrentPolygon
を渡しています。しかし、overlay
はポリゴンなので、私はこのcurrentPolygon
が何であるか分かりません。 rendererForOverlay
はステートレスなので、いくつかのプロパティを参照することをお勧めしませんが、メソッドに渡されたoverlay
を使用してください。そうすることで、複数のポリゴンを持つことができ、地図ビューにどの画像がどのようなものであるかを把握させることができます。
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")
}
出典
2016-05-27 02:38:20
Rob
でロブの答えは、あなたは注釈が正しく追加され、それは 'MKPolygon'だとされていることを確認しました:?だから私は何かのようにしてください例えば。ブレンドポイントを 'rendererForOverlay'に追加し、そのコードを打っていることを確認してください。その場合は、 'drawMapRect'にブレークポイントを追加し、呼び出されていることを確認してください。時にはマップビューのデリゲートを無視するなどの単純なこともあります。このコードについて心配する前にそれがOKであることを確認してみましょう... – Rob
@Robはい、彼らは間違いなく呼び出されています。 'drawMapRect'に進む前に' rendererForOverlay's'リターンを打つでしょう – Jamesar