我在iOS 4上使用iPhone上的mapkit.我使用自定义叠加层和自定义叠加视图,在地图上绘制形状.目前,形状只是矩形,但我正在计划更复杂的东西.这就是我没有使用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);
}
Run Code Online (Sandbox Code Playgroud)
问题是矩形被正确填充(因此看起来它们的边界矩形被正确设置),但它们不会被描边.有人可以帮忙吗?谢谢!
正如之前的一些评论中所报告的,问题在于线宽。更一般地说,所有绘图都会自动缩放以跟随地图缩放,因此如果您希望某些绘图指标与缩放无关,则必须将其除以 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);
}
Run Code Online (Sandbox Code Playgroud)
我还将报告我在叠加层中使用的用于计算和返回边界矩形的代码,因为我认为它可以提供帮助:
-(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);
}
Run Code Online (Sandbox Code Playgroud)
感谢大家的意见和建议。
| 归档时间: |
|
| 查看次数: |
643 次 |
| 最近记录: |