MKMapView图像叠加绘制倒置

时间:2012-12-14 17:04:14

标签: mkmapview cgcontext cgimage mkoverlay mkmaprect

我目前正在创建一个应用程序,需要在地图上绘制大量不同尺寸的叠加层。该应用程序当前抓取2个地图点,这些地图点代表覆盖应该位于的边界框的角落。然后我将这些转换为MKMapRect,以便在MKOverlay类中使用。叠加正在绘制在正确的位置,但图像似乎是垂直翻转的,我可以告诉这一点,因为我尝试应用CGAffineTransformMakeRotation(180)并且图像以正确的方式出现但写入是向后的。

如果有人可以帮我弄清楚它为什么会这样做,我们将不胜感激,请参阅下面的代码。

设置代码:

    // First grab the overlay co-ordinates
    double left = [[self.storedWildMapObject.arrayBBox objectAtIndex:0] doubleValue], bottom = [[self.storedWildMapObject.arrayBBox objectAtIndex:1] doubleValue], right = [[self.storedWildMapObject.arrayBBox objectAtIndex:2] doubleValue], top = [[self.storedWildMapObject.arrayBBox objectAtIndex:3] doubleValue];

// Store these in 2 coordinates representing top left / bot right
    CLLocationCoordinate2D upperLeftCoord =
    CLLocationCoordinate2DMake(top,left);

    CLLocationCoordinate2D lowerRightCoord =
    CLLocationCoordinate2DMake(bottom,right);

//Convert to map points
    MKMapPoint upperLeft = MKMapPointForCoordinate(upperLeftCoord);

    MKMapPoint lowerRight = MKMapPointForCoordinate(lowerRightCoord);

// Work out sizes
    double rightToLeftDiff = lowerRight.y - upperLeft.y;
    double topToBottDiff = lowerRight.x - upperLeft.x;

    MKMapRect bounds = MKMapRectMake(upperLeft.x, upperLeft.y, topToBottDiff, rightToLeftDiff);

// Add to overlay
    MKMapOverlay *mapOverlay = [[MKMapOverlay alloc] initWithMapRect:bounds andCoord:upperLeftCoord];
    [self.mapV addOverlay:mapOverlay];

//地图叠加

- (id)initWithMapRect:(MKMapRect)rect andCoord:(CLLocationCoordinate2D)coord
{
    self = [super init];

    if (self)
    {
        self.centerPos = coord;
        self.mkMapRect = rect;
    }
    return self;
}

-(CLLocationCoordinate2D)coordinate
{
    return self.centerPos;
}

- (MKMapRect)boundingMapRect
{    
    return self.mkMapRect;
}

// MapOverlayView

- (id)initWithOverlay:(id <MKOverlay>)overlay andImage:(UIImage *)img
{
    self = [super initWithOverlay:overlay];
    if (self)
    {
        self.image = img;
    }
    return self;
}

/** Degrees to Radian **/
#define degreesToRadians( degrees ) ( ( degrees ) / 180.0 * M_PI )

/** Radians to Degrees **/
#define radiansToDegrees( radians ) ( ( radians ) * ( 180.0 / M_PI ) )

- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)ctx
{    
    CGImageRef imageReference = self.image.CGImage;

    MKMapRect theMapRect    = [self.overlay boundingMapRect];
    CGRect theRect           = [self rectForMapRect:theMapRect];
    CGRect clipRect     = [self rectForMapRect:mapRect];

    CGContextAddRect(ctx, clipRect);
    CGContextClip(ctx);
    //CGContextRotateCTM(ctx, degreesToRadians(180.0f));

    CGContextDrawImage(ctx, theRect, imageReference);
}

只是因为它有帮助我也有一个调试器打印出来的MapRect,如MapOverlayView中使用的那样:

    (lldb) p theMapRect
(MKMapRect) $17 = {
  (MKMapPoint) origin = {
    (double) x = 1.27662e+08
    (double) y = 7.86099e+07
  }
  (MKMapSize) size = {
    (double) width = 8.12378e+06
    (double) height = 1.27474e+07
  }
}

下面是它的样子:

enter image description here

2 个答案:

答案 0 :(得分:0)

您是否确认storedwildmapobject的坐标符合您期望的顺序?

答案 1 :(得分:0)

这里找到的答案似乎是用相同的mapRect绘制我的图像的正确方法:

https://stackoverflow.com/a/3599448/1090024

所以我现在正在使用它。