如何使用MapKit防止所有UIImage叠加层的旋转

时间:2016-04-19 20:23:02

标签: ios rotation uiimage mapkit mkoverlay

我正在使用MapOverlayRenderer放置叠加层。我首先在地图的视图中使用UIImage并在其视图中旋转它然后转换点(定义boundingMapRect,坐标等),然后将它(Renderer / addOverlay)放置到具有正确插入图像的地图上( CGContextDrawImage)并正确旋转。每次我创建一个新的叠加层[在视图中的UIImage,旋转,转换点,addOverlay)它都可以正常工作。但是,在添加到Map的每个连续叠加层上,代码还会旋转地图上已有的所有其他叠加层。好像我的旋转代码(下面)正在应用于所有叠加层。

我应该制作一个包含boundingMapRect和每个叠加坐标的数组 - 如果是这样,它们应该在viewcontroller中吗? 我应该命名每个叠加层[overlay.title? - 如何设置并调用它?]

感谢一些指导 - 谢谢。

MapOverlayView.h
@interface MapOverlayView ()
@property (nonatomic, strong) UIImage *overlayImage;
@end
@implementation MapOverlayView
- (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];
    IonQuestSingleton *tmp = [IonQuestSingleton sharedSingleton];
    float new_angle = tmp.image_angle;
    NSLog(@"%s%f","New angle is ",new_angle);

    {
    if (new_angle<90) {

            CGContextRotateCTM(context,new_angle*M_PI/180);
            CGContextScaleCTM(context, 1.0, -1.0);
            CGContextTranslateCTM(context, 0.0, -theRect.size.height);
            CGContextDrawImage(context, theRect, imageReference);
            NSLog(@"%s%f","New angle 90 is ",new_angle);//tick

        }
        else if (new_angle >90 && new_angle <=180)
        {
            CGContextRotateCTM(context,new_angle*M_PI/180);
            CGContextScaleCTM(context, 1.0, -1.0);
            CGContextTranslateCTM(context, 0.0, 0.0);
            CGContextDrawImage(context, theRect, imageReference);
            NSLog(@"%s%f","New angle 180 is ",new_angle);//tick

        }
        else if (new_angle >180 && new_angle <=270)
        {
            CGContextRotateCTM(context,new_angle*M_PI/180);
            CGContextScaleCTM(context, 1.0, -1.0);
            CGContextTranslateCTM(context, -theRect.size.width, 0.0);
            CGContextDrawImage(context, theRect, imageReference);
            NSLog(@"%s%f","New angle 270 is ",new_angle);//tick

        }
        else if (new_angle >270) {
            CGContextRotateCTM(context,new_angle*M_PI/180);
            CGContextScaleCTM(context, 1.0, -1.0);
            CGContextTranslateCTM(context, -theRect.size.width, 
-theRect.size.height);
            CGContextDrawImage(context, theRect, imageReference);//tick
            NSLog(@"%s%f","New angle 360 is ",new_angle);
    }
}
}
@end



MapOverlay.h
#import "MapOverlay.h"
#import "IonQuestSingleton.h"

@implementation MapOverlay
#define debug 1

@synthesize boundingMapRect;
@synthesize coordinate;

-(CLLocationCoordinate2D)coordinate {
//Image center point
IonQuestSingleton *tmp = [IonQuestSingleton sharedSingleton];
return CLLocationCoordinate2DMake(tmp.image_ctr_lat,  
tmp.image_ctr_long);
}


- (instancetype)init
{
self = [super init];
if (self) {

    IonQuestSingleton *tmp = [IonQuestSingleton sharedSingleton];

    MKMapPoint upperLeft   =  
MKMapPointForCoordinate(CLLocationCoordinate2DMake(tmp.image_tl_lat,  
tmp.image_tl_long));

    MKMapPoint boundsLeft =  ```MKMapPointForCoordinate(CLLocationCoordinate2DMake(tmp.image_tl_bounds_lat,    tmp.image_tl_bounds_long));

boundingMapRect = MKMapRectMake(upperLeft.x, upperLeft.y,  
fabs(upperLeft.x - boundsLeft.x), fabs(upperLeft.x - boundsLeft.x));
}

return self;
}
@end

Viewcontroller.h

-(void) loadOverlay
{
MapOverlay *overlay = [[MapOverlay alloc] init];
[self.mapView addOverlay:overlay level:MKOverlayLevelAboveLabels];
}

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:
(id<MKOverlay>)overlay {


NSString *mapNameFromMVCPos = [NSString  
stringWithFormat:@"%@%s",self.mapNameFromMVC,"Pos"];//not working

{
    if ([overlay isKindOfClass:[MapOverlay class]]) {

        UIImage *magicMountainImage = [UIImage 
imageNamed:mapNameFromMVCPos];

        MKOverlayRenderer *overlayView = [[MapOverlayView alloc] 
initWithOverlay:overlay overlayImage:magicMountainImage];

        return overlayView;
} etc [evaluate other overlaytypes]

0 个答案:

没有答案