绘图自定义MKOverlay



我使用自定义MKOverlay在MKMapView上绘制天气数据。绘图是在CoreGraphics中完成的。对于这种特殊情况,在drawMapRect:zoomScale:inContext:方法中进行绘图是不够的,因为它是如何处理平铺的。我需要整个图像一次绘制,而不是像drawMapRect方法那样平铺。

之前,我有雷达图像在一个。gif,所以我只是添加了一个imageView,并在drawMapRect中调整imageView帧的大小。

我的计划是用这个做类似的事情。添加一个自定义UIView并在drawMapRect中调用setNeedsDisplay。

以下是相关代码。

MKOverlay的boundingMapRect属性:

- (MKMapRect)boundingMapRect
{
    CLLocationCoordinate2D upperLeftCoord = 
    CLLocationCoordinate2DMake(weatherData.radarArray.connectedRadar.latitude + 2.5,
                           weatherData.radarArray.connectedRadar.longitude - 2.5);
    MKMapPoint upperLeft = MKMapPointForCoordinate(upperLeftCoord);
    CLLocationCoordinate2D lowerRightCoord = 
    CLLocationCoordinate2DMake(weatherData.radarArray.connectedRadar.latitude - 2.5,
                           weatherData.radarArray.connectedRadar.longitude + 2.5);
    MKMapPoint lowerRight = MKMapPointForCoordinate(lowerRightCoord);
    double width = lowerRight.x - upperLeft.x;
    double height = lowerRight.y - upperLeft.y;
    MKMapRect bounds = MKMapRectMake(upperLeft.x, upperLeft.y, width, height);
    return bounds;
}

工作的drawMapRect:zoomScale:inContext: code(太慢了)。

- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context {
    int numPaths = parser.dataPaths.size();
    // We have to pad the map rect a lot to allow for visibility testing that works well.
    MKMapRect testMapRect = MKMapRectMake(mapRect.origin.x - 40000, mapRect.origin.y - 40000, mapRect.size.width + 40000, mapRect.size.height + 40000);;
    // Only draw inside the area we are suppose to
    //CGRect rect = [self rectForMapRect:mapRect];
    //CGContextClipToRect(context, rect);
    // How see through is the radar data. 1 = opaque, 0 = completely transparent
    CGContextSetAlpha(context, 1);
    for (int i = 0; i < numPaths; i++) {
        // Make sure the bin is actually visible in this region before drawing it
        if (MKMapRectContainsPoint(testMapRect, parser.dataPaths[i]->points[0]) ||
            MKMapRectContainsPoint(testMapRect, parser.dataPaths[i]->points[1]) ||
            MKMapRectContainsPoint(testMapRect, parser.dataPaths[i]->points[2]) ||
            MKMapRectContainsPoint(testMapRect, parser.dataPaths[i]->points[3])) {
            CGMutablePathRef path = CGPathCreateMutable();
            CGPoint currentP = [self pointForMapPoint:parser.dataPaths[i]->points[0]];
            CGContextBeginPath(context);
            CGPathMoveToPoint(path, NULL, currentP.x, currentP.y);
            currentP = [self pointForMapPoint:parser.dataPaths[i]->points[1]];
            CGPathAddLineToPoint(path, NULL, currentP.x, currentP.y);
            currentP = [self pointForMapPoint:parser.dataPaths[i]->points[2]];
            CGPathAddLineToPoint(path, NULL, currentP.x, currentP.y);
            currentP = [self pointForMapPoint:parser.dataPaths[i]->points[3]];
            CGPathAddLineToPoint(path, NULL, currentP.x, currentP.y);
            currentP = [self pointForMapPoint:parser.dataPaths[i]->points[0]];
            CGPathAddLineToPoint(path, NULL, currentP.x, currentP.y);
            CGPathCloseSubpath(path);
            CGContextSetFillColorWithColor(context, colors[parser.dataPaths[i]->dataVal]);
            CGContextAddPath(context, path);
            CGContextFillPath(context);
            CGPathRelease(path);
        }
}

新的drawMapRect:zoomScale:inContext: code

- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context {
    // We have to pad the map rect a lot to allow for visibility testing that works well.
    radarImageView.testMapRect = MKMapRectMake(mapRect.origin.x - 40000, mapRect.origin.y - 40000, mapRect.size.width + 40000, mapRect.size.height + 40000);
    radarImageView.frame = [self rectForMapRect:self.overlay.boundingMapRect];
    [radarImageView setNeedsDisplay];
}

自定义UIView的drawRect方法

- (void)drawRect:(CGRect)rect {

    CGContextRef context = UIGraphicsGetCurrentContext();
    int numPaths = parser.dataPaths.size();
    CGContextSetAlpha(context, 1);
    for (int i = 0; i < numPaths; i++) {
        // Make sure the bin is actually visible in this region before drawing it
        if (MKMapRectContainsPoint(testMapRect, parser.dataPaths[i]->points[0]) ||
            MKMapRectContainsPoint(testMapRect, parser.dataPaths[i]->points[1]) ||
            MKMapRectContainsPoint(testMapRect, parser.dataPaths[i]->points[2]) ||
            MKMapRectContainsPoint(testMapRect, parser.dataPaths[i]->points[3])) {
            CGMutablePathRef path = CGPathCreateMutable();
            CGPoint currentP = [(RadarImageOverlayView *)self.superview pointForMapPoint:parser.dataPaths[i]->points[0]];
            CGContextBeginPath(context);
            CGPathMoveToPoint(path, NULL, currentP.x, currentP.y);
            currentP = [(RadarImageOverlayView *)self.superview pointForMapPoint:parser.dataPaths[i]->points[1]];
            CGPathAddLineToPoint(path, NULL, currentP.x, currentP.y);
            currentP = [(RadarImageOverlayView *)self.superview pointForMapPoint:parser.dataPaths[i]->points[2]];
            CGPathAddLineToPoint(path, NULL, currentP.x, currentP.y);
            currentP = [(RadarImageOverlayView *)self.superview pointForMapPoint:parser.dataPaths[i]->points[3]];
            CGPathAddLineToPoint(path, NULL, currentP.x, currentP.y);
            currentP = [(RadarImageOverlayView *)self.superview pointForMapPoint:parser.dataPaths[i]->points[0]];
            CGPathAddLineToPoint(path, NULL, currentP.x, currentP.y);
            CGPathCloseSubpath(path);
            CGContextSetFillColorWithColor(context, colors[parser.dataPaths[i]->dataVal]);
            CGContextAddPath(context, path);
            CGContextFillPath(context);
            CGPathRelease(path);
        }
    }
}

谢谢!

编辑

我认为这个问题与RadarImageView的上下文有关。我在drawRect:方法中获取上下文的方式是否有问题?

我建议大家看看苹果的《HazardMap》样本。它有一些很好的例子来做你所需要的。

KMLViewer也可以提供帮助!

你不能在drawMapRect被调用之前准备好你的路径吗?例如,当可见区域发生变化时。你只需要在drawMapRect中将路径添加到绘图上下文。我认为也许你甚至可以为给定的比例准备路径,然后在区域变化时平移和缩放上下文(CGContextScaleCTM)。

如果数据不经常变化。另一种优化方法是在获得数据后立即为较低的缩放级别准备png格式的图像。对于更高的缩放级别,您可以继续像您这样绘制。

为了减少迭代次数,你可以对数据使用平铺:而不是用一个大数组来存储所有数据,你可以为每个平铺使用一个数组。在第一步中,检索与当前可见区域相交的贴图对应的数组,然后只对这些数组进行循环。当然,这只适用于更高的缩放级别。

如果你不想优化,你可以在显示大量路径的情况下改善用户体验。为了让用户在构建路径时与地图交互,您不应该在一个循环中处理所有元素。您可以一次处理1000个路径,然后使用performSelector:afterDelay:来延迟下一批的处理。这样就可以显示进度条,并让用户与地图进行交互。

最新更新