2011-05-16 57 views
2

新的地圖疊加,但這是一個非常奇怪的問題。iOS - MKOverlayView自定義視圖矩形填充作品,但線條繪製不是

我將mapView.visibleMapRect傳遞給我的覆蓋實現,並將其作爲boundingMapRect返回,現在很好 - 只是試圖在整個地圖上繪製一條直線。

我的drawMapRect被調用,並在下面的代碼中 - 部分透明的綠色矩形被繪製,但行不是。我已經驗證了uiview子類的drawRect中的線條繪製代碼,所以我知道它繪製了一些東西:-)

我確定我必須錯過某件簡單的東西,但我看不到它:-)

引發我興趣的事情是,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 

回答

1

問題是與線寬度的設定。在你的例子中,寬度沒有設置。如果你仍然想解決這個問題,看看這裏Custom MKOverlayView line width

4

需要考慮的一件事是,出於性能原因,在多個線程上調用drawMapRect:。這就解釋了爲什麼你看到drawMapRect:被多次調用。此外,此方法的mapRect輸入不一定等於可見的mapView.visibleMapRect。實際上mapRect通常是mapView.visibleMapRect的一小部分,不同的線程渲染了不同的區域mapView.visibleMapRect。因此,僅僅因爲一個點包含在mapView.visibleMapRect之內,它並不意味着它包含在mapRect中傳遞給所有調用drawMapRect的結果。

0

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