0

I am currently creating an app which requires numerous overlays of different sizes to be drawn on a map. The app currently grabs 2 map points which represent the corners of a bounding box in which the overlay should sit. I am then converting these in to a MKMapRect for use within an MKOverlay class. The overlay is drawing in the correct place but the image seems to be flipped vertically, I can tell this as I tried applying a CGAffineTransformMakeRotation(180) and the image appeared the right way around but the writing was backwards.

If anyone can help me figure out why it's doing this it would be much appreciated, please see my code below.

Setup code:

    // 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];

// Map overlay

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

Just incase it helps I also have a debugger print out of theMapRect as used in 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
  }
}

Below is what it looks like:

enter image description here

Elliott D'Alvarez
  • 1,217
  • 16
  • 30

2 Answers2

0

Have you verified that the storedwildmapobject has the coordinates in the order you are expecting them?

Craig
  • 8,093
  • 8
  • 42
  • 74
  • Sorry for the slow reply, should never post questions before a busy weekend! I'm pretty sure the co-ordinates are in the right order, the upperLeft has the higher of the lats and the lower of the lons. It does map in the correct place when given the upperLeft co-ord and the size, so I presume this is correct as both the width and height are positive? Or should the height be negative as the bottomRight co-ord should have a lower lat? – Elliott D'Alvarez Dec 17 '12 at 10:11
0

The answer found here seems to draw my image the correct way around with the same mapRect:

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

So I'm using this for now.

Community
  • 1
  • 1
Elliott D'Alvarez
  • 1,217
  • 16
  • 30