2015-12-12 70 views
0

我想在國家邊界上繪製一個國家的旗幟。我使用從here在MKMapView上繪製標誌作爲覆蓋圖

我加入疊加的方式座標數據是

[mapView addOverlays:countryName]; 

然後使其作爲

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay 
{ 
    if ([overlay isKindOfClass:[MKPolygon class]]) 
    { 
    MKPolygonRenderer *renderer = [[MKPolygonRenderer alloc] initWithPolygon:overlay]; 
    renderer.fillColor  = [ColorUtil colorFromHexString:@"#EE5A43" withAlpha:0.6]; 
    renderer.strokeColor = [[UIColor whiteColor] colorWithAlphaComponent:1.0]; 
    renderer.lineWidth = 2; 
    return renderer; 
    } 
    return nil; 
} 

現在我的想法是畫了標誌的疊加。所以我分類MKPolygonRenderer,並添加

@implementation MyMKOverlayRenderer 
- (instancetype)initWithOverlay:(id<MKOverlay>)overlay overlayImage:(UIImage *)overlayImage { 
    self = [super initWithOverlay:overlay]; 
    if (self) { 
    _overlayImage = overlayImage; 
    } 

    return self; 
} 
- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context { 
    CGImageRef imageReference = self.overlayImage.CGImage; 

    MKMapRect theMapRect = self.overlay.boundingMapRect; 
    CGRect theRect = [self rectForMapRect:theMapRect]; 

    CGContextScaleCTM(context, 1.0, -1.0); 
    CGContextTranslateCTM(context, 0.0, -theRect.size.height); 
    CGContextDrawImage(context, theRect, imageReference); 
} 
@end 

現在檢查兩個附加的圖像。我期待旗子在多邊形的邊界內。但看起來並沒有發生。所以可能是我的做法是錯誤的。有些專家可以幫助我正確的方向。

without drawing flag

drawing flag

回答

0

我解決了這個問題。關鍵是要創建一個UIBezierPath並剪切覆蓋圖像。

我已經發布了完整的解決方案here

現在看起來

enter image description here