gpt4 book ai didi

iphone - 在自定义 MKOverlay 上绘图

转载 作者:行者123 更新时间:2023-12-03 18:57:33 29 4
gpt4 key购买 nike

我正在使用自定义 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:代码(太慢了)。

- (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:代码

- (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苹果公司的 sample 。它有一些很好的例子,可以完全按照您的要求进行操作。

KMLViewer也可能有帮助!

关于iphone - 在自定义 MKOverlay 上绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6727668/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com