0

I draw an image on my map as overlay like so (RW example code):

It has a transparent part in the middle (that should let touches through) and non-transparent sides that should catch touches (kind of like a donut :)).

Overlay.h

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>
@class PVPark;
@interface PVParkMapOverlay : NSObject <MKOverlay>
- (instancetype)initWithPark:(PVPark *)park;
@end

Overlay.m

#import "PVParkMapOverlay.h"
#import "PVPark.h"
 @implementation PVParkMapOverlay
@synthesize coordinate;
@synthesize boundingMapRect;
- (instancetype)initWithPark:(PVPark *)park {
self = [super init];
if (self) {
    boundingMapRect = park.overlayBoundingMapRect;
    coordinate = park.midCoordinate;
}
return self;
}
@end

OverlayView.h

#import <MapKit/MapKit.h>
@interface PVParkMapOverlayView : MKOverlayRenderer
- (instancetype)initWithOverlay:(id<MKOverlay>)overlay overlayImage:(UIImage *)overlayImage;
@end

OverlayView.m

#import "PVParkMapOverlayView.h"
@interface PVParkMapOverlayView ()
@property (nonatomic, strong) UIImage *overlayImage;
@end
@implementation PVParkMapOverlayView
- (instancetype)initWithOverlay:(id<MKOverlay>)overlay overlayImage:(UIImage *)overlayImage {
self = [super initWithOverlay:overlay];
if (self) {
    _overlayImage = overlayImage;
}

return self;
}
- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context {
CGImageRef imageReference = self.overlayImage.CGImage;

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

CGContextScaleCTM(context, 1.0, -1.0);
CGContextTranslateCTM(context, 0.0, -theRect.size.height);
CGContextDrawImage(context, theRect, imageReference);
}
@end

I add it to the map like so:

MainViewController

- (void) viewDidLoad
{
[super viewDidLoad];
[self addOverlay];
}

- (void)addOverlay {
PVParkMapOverlay *overlay = [[PVParkMapOverlay alloc] initWithPark:self.park];
[self.mapView addOverlay:overlay];
}



- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {
if ([overlay isKindOfClass:PVParkMapOverlay.class]) {
    UIImage *magicMountainImage = [UIImage imageNamed:@"overlay_park"];
    PVParkMapOverlayView *overlayView = [[PVParkMapOverlayView alloc] initWithOverlay:overlay overlayImage:magicMountainImage];

    return overlayView;
}
return nil;
}

How can I detect a touch on the non-transparent part of the overlayed image? I've tried using some examples using MKPolyLines but to no avail. Should I create an invisible (to the user) polyline using the cutout's location data and check for that? Seems like there should be a way to catch the overlay more efficiently, no?

BossBols
  • 175
  • 12
  • I think creating an "invisible" polyline (polygon actually) and checking whether a point is in the polygon _is_ the most efficient and practical way here (especially if you know the coordinates of the area). See http://stackoverflow.com/questions/25835985/determine-whether-a-cllocationcoordinate2d-is-within-a-defined-region-bounds. –  Dec 16 '14 at 02:38
  • Thanks for the sample. It's definitely an option but the middle transparent opening is very squiggly and rarely with straight lines so taking all those coords would take a lot of time to get it just right. Or is there an easy and quick way to get a hold of all these points on Maps? – BossBols Dec 17 '14 at 16:03

0 Answers0