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

发布于 2024-11-07 14:48:40 字数 7242 浏览 1 评论 0原文

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

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

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

我确信我一定错过了一些简单的东西,但我看不到它:-)让

我困惑的东西关闭的是,当转换为 CGRect 时,visibleMapRect 有非常奇怪的坐标 - 它们对我来说根本不像屏幕坐标 - 这对我来说似乎是问题的根源。

任何帮助将不胜感激:

drawMapRect 被多次调用,我在下面附上输出。

谢谢, Bob

ViewController 设置代码:

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];

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;

}

MyOverlayView 绘图代码

- (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

New to Map Overlays, but this is a really weird problem.

I pass in the mapView.visibleMapRect to my overlay implementation and return that as the boundingMapRect, which is fine for now - just trying to draw a line on the whole map rect.

My drawMapRect is getting called, and in the following code - the partially transparent green rectangle is drawn, but the lines are not. I've verified the line drawing code in drawRect of a uiview subclass, so I know it draws something :-)

I'm sure I must be missing something easy, but I can't see it :-)

The thing that throws me off is that the visibleMapRect, when converted to a CGRect has very strange coordinates - they don't look like screen coordinates to me at all - which seems like the source of the problem to me.

Any help would be greatly appreciated:

The drawMapRect is called multiple times, i'm attaching the output below.

Thanks,
Bob

ViewController setup code:

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];

MyOverlay code:

@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;

}

MyOverlayView drawing code

- (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);

}

Output

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忆梦 2024-11-14 14:48:40

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

One thing to consider is that for performance reasons, drawMapRect: is called on multiple threads. This explains why you see drawMapRect: getting called multiple times. Additionally the mapRect input to this method is NOT necessarily equal to the visible mapView.visibleMapRect. In fact mapRect is usually a small section of mapView.visibleMapRect and different threads render different regions of mapView.visibleMapRect. Thus just because a point is contained inside mapView.visibleMapRect it does not mean that it is contained within the mapRect passed to all of the resulting calls to drawMapRect.

对你的占有欲 2024-11-14 14:48:40

您在调用 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));

You have the wrong coordinates in your call to CGContextAddLineToPoint. You have:

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

but it should be:

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

At least if you are hoping to draw a line diagonally across your rectangle.

Also as Ludvik Polak mentioned above, you probably also need to set the line width using something like:

CGContextSetLineWidth(context, MKRoadWidthAtZoomScale(zoomScale));
北斗星光 2024-11-14 14:48:40

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

I have come across this when there is another MKTileOverlay loading on the same map. It will cause the custom overlay to refresh until a tile is drawn by the other overlay, which may take several seconds in case of network load.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文