2013-12-22 128 views
0

我有一個地圖可以創建路徑並將它們存儲在數據庫中。當應用程序重新加載時,任何已保存的路徑將作爲覆蓋再次加載它工作良好,直到我放大,我發現它們沒有正確調整大小。我如何強制重繪來糾正大小。我收錄了我所看到的屏幕截圖。如何重繪地圖上的覆蓋

enter image description here

這裏是我的代碼示例。它繪製每個點的子路徑。類似於用戶位置後面的breadcrump。這是在自定義的重疊類,並呼籲在

-(void)drawMapRect:(MKMapRect)mapRect 
      zoomScale:(MKZoomScale)zoomScale 
      inContext:(CGContextRef)context 
{ 


for (int io=0;io<OverlayList.count;io++){ 
     CrumbPath *crumbs = [OverlayList objectAtIndex:io]; 

      NSMutableArray *colors = [NSMutableArray arrayWithCapacity:2]; 
      UIColor *color = [UIColor redColor]; 
      int speedcolor; 
      UIColor *currentColor = [UIColor colorWithRed:255/255.0f green:256/255.0f blue:0/255.0f alpha:1.0f]; 
      UIColor *NewcurrentColor = [UIColor colorWithRed:255/255.0f green:256/255.0f blue:0/255.0f alpha:1.0f]; 

      [colors addObject:(id)[color CGColor]]; 

      for (int i=0;i<crumbs.pointCount;i++){ 
       NSInteger CurrentSpeed=crumbs.pointSpeed[i]; 
       speedcolor=255-((int)CurrentSpeed*3); 

       if (speedcolor<2){ 
        speedcolor=1; 
       } 

       CGMutablePathRef subpath = CGPathCreateMutable(); 
       CGPoint point = [self pointForMapPoint:crumbs.points[i]]; 
       if (i==0){ 
        CGPathMoveToPoint(subpath, nil, point.x, point.y); 
        CGPathRelease(subpath); 
       } else { 

        CGPoint prevPoint = [self pointForMapPoint:crumbs.points[i-1]]; 
        CGPathMoveToPoint(subpath, nil, prevPoint.x, prevPoint.y); 
        CGPathAddLineToPoint(subpath, nil, point.x, point.y); 

        CGFloat lineWidth = MKRoadWidthAtZoomScale(zoomScale); 

        CGFloat gradientLocation[2] = {0,1}; 
        CGContextSaveGState(context); 

        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); 

        CGPoint gradientStart = prevPoint; 
        CGPoint gradientEnd = point; 
        CGGradientRef gradient; 

        NewcurrentColor = [UIColor colorWithRed:255/255.0f green:speedcolor/255.0f blue:0/255.0f alpha:1.0f]; 

        NSMutableArray *colors = [NSMutableArray arrayWithCapacity:2]; 
        UIColor *color; 
        [colors addObject:(id)[currentColor CGColor]]; 
        color = NewcurrentColor; 
        [colors addObject:(id)[color CGColor]]; 

        gradient = CGGradientCreateWithColors(colorSpace, (CFArrayRef)colors, gradientLocation); 


        CGPathRef pathToFill = CGPathCreateCopyByStrokingPath(subpath, NULL, lineWidth, kCGLineCapRound, kCGLineJoinBevel, 0); 
        CGContextAddPath(context, pathToFill); 

        //NSLog(@"%f",lineWidth); 

        CGContextClip(context);//<--clip your context after you SAVE it, important! 
        CGContextDrawLinearGradient(context, gradient, gradientStart, gradientEnd, kCGGradientDrawsBeforeStartLocation); 

        CGContextRestoreGState(context);//<--Don't forget to restore your context. 

        currentColor = NewcurrentColor; 
        CGGradientRelease(gradient); 
        CGPathRelease(pathToFill); 
        CGColorSpaceRelease(colorSpace); 
        CGPathRelease(subpath); 

       } 

     } 
    } 
+0

我們可以使用更多關於如何繪製路徑的信息。如果您顯示一些繪圖代碼,這將是最好的。 – Sti

+0

好點。我編輯了我的原創。 – jdross

+0

所以你不使用MKPolyline?如果你這樣做了,每次都會在地圖視圖中重繪路徑,並且在每一步都會平滑。 – Craig

回答

1

在我看來,因爲你正在使用-drawMapRect:zoomScale:inContext:,您還需要響應使用–setNeedsDisplayInMapRect:zoomScale:來縮放地圖(通過委託回調)。然後你的渲染器會再次被調用來更新它的繪製。