我在地图上绘制一个图像,如同覆盖一样(RW示例代码):
中间有一个透明的部分(应该让它接触)和不透明的边应该接触(有点像甜甜圈:)。
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
我将其添加到地图中,如下所示:
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;
}
如何在覆盖图像的非透明部分检测到触摸?我尝试过使用MKPolyLines的一些例子,但无济于事。我应该使用剪切的位置数据创建一个不可见的(对用户)折线并检查它吗?似乎应该有一种方法来更有效地捕捉叠加层,不是吗?