Question

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

Was it helpful?

Solution 2

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top