2011-11-25 8 views
5

iOS 4のiPhoneでマップキットを使用しています。 カスタムオーバーレイビューとカスタムオーバーレイビューを使用してマップ上に図形を描画しています。 現在のところ、図形は長方形ですが、私はもっと洗練されたものを計画しています。これが私がMKPolygonオーバーレイタイプを使用していない理由です。 これは私のオーバーレイビューの描画メソッドのコードです:マップキックオーバーレイビューのパスをストロークできません

-(void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context 
{ 
    // Clip context to bounding rectangle 
    MKMapRect boundingMapRect = [[self overlay] boundingMapRect]; 
    CGRect boundingRect = [self rectForMapRect:boundingMapRect]; 
    CGContextAddRect(context, boundingRect); 
    CGContextClip(context); 

    // Define shape 
    CGRect shapeRect = CGRectMake(0.5f, 0.5f, boundingRect.size.width - 1.0f, boundingRect.size.height - 1.0f); 

    // Fill 
    CGContextSetRGBFillColor(context, 0.5f, 0.5f, 0.5f, 0.5f); 
    CGContextFillRect(context, shapeRect); 

    // Stroke 
    CGContextSetRGBStrokeColor(context, 0, 0, 0, 0.75f); 
    CGContextSetLineWidth(context, 1.0f); 
    CGContextStrokeRect(context, shapeRect); 
} 

問題は、長方形が正しく満たされ得るということである(彼らの境界矩形が正しく設定されて表示されます)、しかし、彼らはストロークされません。 誰も助けることができますか? ありがとう!

+0

解決策がわかりませんが、私は[CGRectInset](http://developer.apple.com/library/ios/DOCUMENTATION/GraphicsImaging/Reference/CGGeometry/Reference/reference)に注目したいと思います。 html#// apple_ref/c/func/CGRectInset)(CGRectMakeで長方形を作成するのではなく)。 – DarkDust

+0

ちょうど私に起こった:あなたはそれをさらにインセットすると、矩形はストロークされますか?だから最初にあなたはそれを塗りつぶし、その後、別の0.5でrectを差し込み、そしてそれを打ちます。 – DarkDust

+0

これに関連する別の質問が見つかりました:http://stackoverflow.com/questions/5274164/custom-mkoverlayview-line-width問題は線幅のスケーリングと思われ、私のコードをこのように修正する必要があるようですCGContextSetLineWidth(context、5.0f/zoomScale); –

答えて

2

これまでのいくつかのコメントで報告されているように、問題は線幅にあります。より一般的には、すべての描画がマップのズームに合わせて自動的にスケーリングされるため、一部の描画メトリックをズームに依存しないようにするには、zoomScaleで分割する必要があります。ここで

は私のiPhone 4上で正しく動作する新しいコードを、次のとおりです。

-(void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context 
{ 
    // Clip context to bounding rectangle 
    MKMapRect boundingMapRect = [[self overlay] boundingMapRect]; 
    CGRect boundingRect = [self rectForMapRect:boundingMapRect]; 
    CGContextAddRect(context, boundingRect); 
    CGContextClip(context); 

    // Define shape 
    CGRect shapeRect = CGRectInset(boundingRect, 2.0f/zoomScale, 2.0f/zoomScale); 

    // Fill 
    CGContextSetRGBFillColor(context, 0.5f, 0.5f, 0.5f, 0.5f); 
    CGContextFillRect(context, shapeRect); 

    // Stroke 
    CGContextSetRGBStrokeColor(context, 0, 0, 0, 0.75f); 
    CGContextSetLineWidth(context, 4.0f/zoomScale); 
    CGContextStrokeRect(context, shapeRect); 
} 

私はそれができると思うので、私はまた、私は外接する四角形を計算して返すためにオーバーレイに使用していたコードを報告します。ヘルプ:

-(MKMapRect)boundingMapRect 
{ 
    // Overlay bounds 
    CLLocationCoordinate2D topLeftcoordinate = <the top-left coordinate of overlay>; 
    CLLocationCoordinate2D bottomRightCoordinate = <the bottom-right coordinate of overlay>; 

    // Convert them to map points 
    MKMapPoint topLeftPoint = MKMapPointForCoordinate(topLeftcoordinate); 
    MKMapPoint bottomRightPoint = MKMapPointForCoordinate(bottomRightCoordinate); 

    // Calculate map rect 
    return MKMapRectMake(topLeftPoint.x, topLeftPoint.y, bottomRightPoint.x - topLeftPoint.x, topLeftPoint.y - bottomRightPoint.y); 
} 

ご意見ありがとうございます。

関連する問題