如何在地图上重绘叠加层

时间:2013-12-22 17:20:09

标签: ios objective-c mapkit

我有一张地图可以创建路径并将它们存储在数据库中。当重新加载应用程序时,任何已保存的路径将再次作为叠加层加载它工作正常,直到我放大,我发现它们没有正确调整大小。如何强制重绘以更正大小。我包括了我所看到的屏幕截图。

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

                }

        }
    }

1 个答案:

答案 0 :(得分:1)

在我看来,由于您使用-drawMapRect:zoomScale:inContext:,因此您还需要使用–setNeedsDisplayInMapRect:zoomScale:来响应缩放地图(通过委托回调)。然后,您的渲染器将再次被调用以更新其绘制。