iOS - MKOverlayView自定义视图矩形填充有效,但线条绘制无效



地图叠加的新手,但这是一个非常奇怪的问题。

我将 mapView.visibleMapRect 传递给我的覆盖实现,并将其作为 boundingMapRect 返回,现在很好 - 只是尝试在整个地图矩形上画一条线。

我的 drawMapRect 被调用,在下面的代码中 - 绘制了部分透明的绿色矩形,但没有绘制线条。 我已经验证了 uiview 子类的 drawRect 中的线条绘制代码,所以我知道它绘制了一些东西:-)

确定我一定错过了一些容易的东西,但我看不到它:-)

让我失望的是,当转换为CGRect时,visibleMapRect具有非常奇怪的坐标 - 它们对我来说根本不像屏幕坐标 - 这对我来说似乎是问题的根源。

任何帮助将不胜感激:

drawMapRect被多次调用,我附加下面的输出。

谢谢鲍勃

视图控制器设置代码:

CLLocationCoordinate2D center;
center.latitude = 29.46;
center.longitude = -98.30;
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = 0.2;
span.longitudeDelta = 0.2;
region.center = center;
region.span = span;
self.mapView.region = region;
MyOverlay *myoverlay = [[MyOverlay alloc] initWithCoordinate: center andBoundingRect: self.mapView.visibleMapRect];
[self.mapView addOverlay:myoverlay];

我的覆盖代码:

@implementation MyOverlay
@synthesize coordinate;
@synthesize boundingMapRect;

- (id) initWithCoordinate: (CLLocationCoordinate2D) coord andBoundingRect: (MKMapRect) bRect {
    if ((self = [super init])) {
        //customize here
        coordinate = coord;
        boundingMapRect = bRect;
    }
    return self;
}

我的叠加视图绘图代码

- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context {
    CGRect theRect = [self rectForMapRect:mapRect];
    NSLog(@"in drawMapRect ...theRect is %f, %f, size of %f x %f", theRect.origin.x, theRect.origin.y, theRect.size.width, theRect.size.height); 
    CGContextSetAlpha(context, 1.0);
    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
    CGContextMoveToPoint(context, theRect.origin.x, theRect.origin.y);
    CGContextAddLineToPoint(context, theRect.size.width, theRect.size.width);
    CGContextSetAlpha(context, .25);
    CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
    CGContextFillRect(context, theRect);
}

输出

2011-05-16 11:25:18.037 coolOverlayTest[71661:7903] in drawMapRect ...theRect is 61440.000000, 37888.000000, size of 262144.000000 x 262144.000000
2011-05-16 11:25:18.038 coolOverlayTest[71661:760b] in drawMapRect ...theRect is 323584.000000, 37888.000000, size of 262144.000000 x 262144.000000
2011-05-16 11:25:18.042 coolOverlayTest[71661:7903] in drawMapRect ...theRect is 323584.000000, 37888.000000, size of 262144.000000 x 262144.000000
2011-05-16 11:25:18.044 coolOverlayTest[71661:760b] in drawMapRect ...theRect is -200704.000000, 37888.000000, size of 262144.000000 x 262144.000000
2011-05-16 11:25:18.046 coolOverlayTest[71661:7903] in drawMapRect ...theRect is -200704.000000, 37888.000000, size of 262144.000000 x 262144.000000
2011-05-16 11:25:18.048 coolOverlayTest[71661:760b] in drawMapRect ...theRect is 61440.000000, -224256.000000, size of 262144.000000 x 262144.000000
2011-05-16 11:25:18.052 coolOverlayTest[71661:760b] in drawMapRect ...theRect is 323584.000000, -224256.000000, size of 262144.000000 x 262144.000000
2011-05-16 11:25:18.054 coolOverlayTest[71661:7903] in drawMapRect ...theRect is 323584.000000, -224256.000000, size of 262144.000000 x 262144.000000
2011-05-16 11:25:18.056 coolOverlayTest[71661:760b] in drawMapRect ...theRect is -200704.000000, -224256.000000, size of 262144.000000 x 262144.000000
2011-05-16 11:25:18.057 coolOverlayTest[71661:7903] in drawMapRect ...theRect is -200704.000000, -224256.000000, size of 262144.000000 x 262144.000000
2011-05-16 11:25:18.059 coolOverlayTest[71661:760b] in drawMapRect ...theRect is 61440.000000, 300032.000000, size of 262144.000000 x 262144.000000
2011-05-16 11:25:18.061 coolOverlayTest[71661:7903] in drawMapRect ...theRect is 61440.000000, 300032.000000, size of 262144.000000 x 262144.000000
2011-05-16 11:25:18.063 coolOverlayTest[71661:760b] in drawMapRect ...theRect is 323584.000000, 300032.000000, size of 262144.000000 x 262144.000000
2011-05-16 11:25:18.064 coolOverlayTest[71661:7903] in drawMapRect ...theRect is 323584.000000, 300032.000000, size of 262144.000000 x 262144.000000
2011-05-16 11:25:18.066 coolOverlayTest[71661:760b] in drawMapRect ...theRect is -200704.000000, 300032.000000, size of 262144.000000 x 262144.000000
2011-05-16 11:25:18.068 coolOverlayTest[71661:7903] in drawMapRect ...theRect is -200704.000000, 300032.000000, size of 262144.000000 x 262144.000000
2011-05-16 11:25:18.070 coolOverlayTest[71661:760b] in drawMapRect ...theRect is 585728.000000, 37888.000000, size of 262144.000000 x 262144.000000
2011-05-16 11:25:18.072 coolOverlayTest[71661:7903] in drawMapRect ...theRect is 585728.000000, 37888.000000, size of 262144.000000 x 262144.000000
2011-05-16 11:25:18.074 coolOverlayTest[71661:760b] in drawMapRect ...theRect is -462848.000000, 37888.000000, size of 262144.000000 x 262144.000000
2011-05-16 11:25:18.075 coolOverlayTest[71661:7903] in drawMapRect ...theRect is -462848.000000, 37888.000000, size of 262144.000000 x 262144.000000
2011-05-16 11:25:18.082 coolOverlayTest[71661:7903] in drawMapRect ...theRect is 61440.000000, -486400.000000, size of 262144.000000 x 262144.000000
2011-05-16 11:25:18.082 coolOverlayTest[71661:760b] in drawMapRect ...theRect is 585728.000000, -224256.000000, size of 262144.000000 x 262144.000000
2011-05-16 11:25:18.085 coolOverlayTest[71661:760b] in drawMapRect ...theRect is 61440.000000, -486400.000000, size of 262144.000000 x 262144.000000
2011-05-16 11:25:18.087 coolOverlayTest[71661:7903] in drawMapRect ...theRect is -462848.000000, -224256.000000, size of 262144.000000 x 262144.000000
2011-05-16 11:25:18.089 coolOverlayTest[71661:760b] in drawMapRect ...theRect is -462848.000000, -224256.000000, size of 262144.000000 x 262144.000000
2011-05-16 11:25:18.099 coolOverlayTest[71661:760b] in drawMapRect ...theRect is 585728.000000, 300032.000000, size of 262144.000000 x 262144.000000
2011-05-16 11:25:18.102 coolOverlayTest[71661:760b] in drawMapRect ...theRect is -462848.000000, 300032.000000, size of 262144.000000 x 262144.000000
2011-05-16 11:25:18.106 coolOverlayTest[71661:760b] in drawMapRect ...theRect is 323584.000000, -486400.000000, size of 262144.000000 x 262144.000000
2011-05-16 11:25:18.109 coolOverlayTest[71661:760b] in drawMapRect ...theRect is -200704.000000, -486400.000000, size of 262144.000000 x 262144.000000
2011-05-16 11:25:18.111 coolOverlayTest[71661:7903] in drawMapRect ...theRect is -200704.000000, -486400.000000, size of 262144.000000 x 262144.000000
2011-05-16 11:25:18.116 coolOverlayTest[71661:760b] in drawMapRect ...theRect is 585728.000000, -486400.000000, size of 262144.000000 x 262144.000000
2011-05-16 11:25:18.119 coolOverlayTest[71661:7903] in drawMapRect ...theRect is 585728.000000, -486400.000000, size of 262144.000000 x 262144.000000
2011-05-16 11:25:18.124 coolOverlayTest[71661:7903] in drawMapRect ...theRect is -462848.000000, -486400.000000, size of 262144.000000 x 262144.000000
2011-05-16 11:25:18.126 coolOverlayTest[71661:760b] in drawMapRect ...theRect is -462848.000000, -486400.000000, size of 262144.000000 x 262144.000000

要考虑的一件事是,出于性能原因,drawMapRect: 是在多个线程上调用的。这就解释了为什么您看到drawMapRect:被多次调用。此外,此方法的mapRect输入不一定等于可见mapView.visibleMapRect。实际上,mapRect通常是mapView.visibleMapRect的一小部分,不同的线程呈现mapView.visibleMapRect的不同区域。因此,仅仅因为一个点包含在mapView.visibleMapRect并不意味着它包含在传递给所有结果drawMapRect调用的mapRect中。

您在调用 CGContextAddLineToPoint 时有错误的坐标。你有:

CGContextMoveToPoint(context, theRect.origin.x, theRect.origin.y);
CGContextAddLineToPoint(context, theRect.size.width, theRect.size.width);

但它应该是:

CGContextMoveToPoint(context, theRect.origin.x, theRect.origin.y);
CGContextAddLineToPoint(context, theRect.origin.x + theRect.size.width, theRect.origin.y + theRect.size.height);

至少如果您希望在矩形上对角线画一条线。

同样,正如Ludvik Polak上面提到的,您可能还需要使用以下方法设置线宽:

CGContextSetLineWidth(context, MKRoadWidthAtZoomScale(zoomScale));

当在同一张地图上加载另一个 MKTileOverlay 时,我遇到了这个问题。这将导致自定义叠加层刷新,直到另一个叠加层绘制一个图块,这在网络负载的情况下可能需要几秒钟。

相关内容

最新更新