1

我正在尝试创建一个自定义MKOverlayView类,该类自正常以来绘制覆盖,MKOverlay并且MKOverlayView不适合我正在构建的应用程序的需求。

我在绘制多边形叠加层时遇到问题;它看起来没有我让MKOverlayView画出来的那么好,我的意思是边缘不清晰,当你放大地图时都是像素化的。由于某种原因,从点到点的线也不会被绘制。

此外,在放大某些多边形时,绘图会被剪掉,直到我再次缩小。

这是我的绘图代码

- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context{
MapOverlay *newOverlay = (MapOverlay *)self.overlay;
CGColorRef ref2 = [newOverlay.strokeColor CGColor];
CGContextSetLineWidth(context, newOverlay.lineWidth);
CGColorRef ref = [newOverlay.fillColor CGColor];

int _countComponents = CGColorGetNumberOfComponents(ref);

if (_countComponents == 4) {
    const CGFloat *_components2 = CGColorGetComponents(ref);
    CGFloat red     = _components2[0];
    CGFloat green = _components2[1];
    CGFloat blue   = _components2[2];
    CGFloat alpha = _components2[3];

    CGContextSetRGBFillColor(context, red, green, blue, alpha);

}

for (int i=0; i<newOverlay.coordinates.count; i++){
    if(i % 2 == 0) {
        CLLocationCoordinate2D p;
        p.latitude =  (CLLocationDegrees)[(NSString *)[newOverlay.coordinates objectAtIndex:i+1]floatValue];
        p.longitude =  (CLLocationDegrees)[(NSString *)[newOverlay.coordinates objectAtIndex:i] floatValue];
        //CLLocation *p = [[CLLocation alloc] initWithLatitude:(CLLocationDegrees)[(NSString *)[newOverlay.coordinates objectAtIndex:i+1] floatValue] longitude:(CLLocationDegrees)[(NSString *)[newOverlay.coordinates objectAtIndex:i] floatValue]];
        MKMapPoint point = MKMapPointForCoordinate(p);
        CGPoint point2 = [self pointForMapPoint:point];
        if (i==0){
            CGContextMoveToPoint(context, point2.x, point2.y);
        }else{
            CGContextAddLineToPoint(context, point2.x, point2.y);
        }
    }

}
CGContextDrawPath(context, kCGPathFillStroke);
//CGContextStrokePath(context);
}

我发现关于自定义类以及如何在地图上绘制的信息很少,MKOverlayView但这些是我用来执行此操作的 2 个教程

http://spitzkoff.com/craig/?p=65

http://i.ndigo.com.br/2012/05/ios-maps-with-image-overlays/

更新

我有一种感觉,它可能与叠加层的边界框有关,因为如果我返回bounding box of the world它,它会显示得很好,但绘制所有可能的叠加层显然效率不高。

这是我在自定义 MKOverlay 类中找到叠加层边界框的方法

- (MKMapRect)boundingMapRect{

double maxY = 0;
double minY = 0;
double maxX = 0;
double minX = 0;

for(NSUInteger i = 0;i < coordinates.count; i++) {
    if(i % 2 == 0) {
        CLLocationCoordinate2D tempLoc;
        tempLoc.latitude =(CLLocationDegrees)[(NSString *)[coordinates objectAtIndex:(NSUInteger)i + 1] floatValue];

        tempLoc.longitude = (CLLocationDegrees)[(NSString *)[coordinates objectAtIndex:(NSUInteger)i] floatValue];

        MKMapPoint tempPoint = MKMapPointForCoordinate(tempLoc);
        if(i == 0){
            minX = tempPoint.x;
            minY = tempPoint.y;
            if(tempPoint.x > maxX){
                maxX = tempPoint.x;
            }
            if (tempPoint.y > maxY){
                maxY = tempPoint.y;
            }
        }else{
            if(tempPoint.x > maxX){
                maxX = tempPoint.x;
            }
            if(tempPoint.x < minX){
                minX = tempPoint.x;
            }
            if (tempPoint.y > maxY){
                maxY = tempPoint.y;
            }
            if(tempPoint.y < minY){
                minY = tempPoint.y;
            }
        }

    }
}

MKMapRect b2 = MKMapRectMake(minX, maxY, minX-maxX, minY-maxY);

return b2;
//return MKMapRectWorld;
}
4

0 回答 0