iOS — заполнение прямоугольников пользовательского представления MKOverlayView работает, но рисование линий не работает

Новичок в Map Overlays, но это действительно странная проблема.

Я передаю mapView.visibleMapRect в свою реализацию наложения и возвращаю его как boundingMapRect, что пока нормально — просто пытаюсь нарисовать линию на всей карте.

Мой drawMapRect вызывается, и в следующем коде частично прозрачный зеленый прямоугольник рисуется, а линии нет. Я проверил код рисования линий в drawRect подкласса uiview, поэтому я знаю, что он что-то рисует :-)

Я уверен, что я должен что-то упустить, но я не вижу этого :-)

Что меня сбивает с толку, так это то, что visibleMapRect при преобразовании в CGRect имеет очень странные координаты - они совсем не похожи на экранные координаты для меня - что кажется мне источником проблемы.

Любая помощь будет принята с благодарностью:

DrawMapRect вызывается несколько раз, я прикрепляю вывод ниже.

Спасибо, Боб

Код установки 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

person Bink    schedule 16.05.2011    source источник


Ответы (2)


Следует учитывать, что по соображениям производительности drawMapRect: вызывается в нескольких потоках. Это объясняет, почему вы видите, что drawMapRect: вызывается несколько раз. Кроме того, ввод mapRect для этого метода НЕ обязательно равен видимому mapView.visibleMapRect. На самом деле mapRect обычно представляет собой небольшую часть mapView.visibleMapRect, и разные потоки отображают разные области mapView.visibleMapRect. Таким образом, то, что точка содержится внутри mapView.visibleMapRect, не означает, что она содержится внутри mapRect, передаваемого всем результирующим вызовам drawMapRect.

person Jeff Ames    schedule 12.12.2012

У вас неправильные координаты в вызове 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);

По крайней мере, если вы надеетесь провести линию по диагонали через прямоугольник.

Кроме того, как упоминал выше Людвик Полак, вам, вероятно, также необходимо установить ширину линии, используя что-то вроде:

CGContextSetLineWidth(context, MKRoadWidthAtZoomScale(zoomScale));
person EarlyRiser    schedule 15.01.2013