自定义MKOverlayView

如果上面三种还是不能满足要求,那么就需要自定义了。这个例子我是学习的别人的代码。

MapOverlay实现协议MKOverlay。

MapOverlayView继承MKOverlayView。

下面是代码

@interfaceMapOverlay :NSObject {

}

- (MKMapRect)boundingMapRect;

@property(nonatomic,readonly)CLLocationCoordinate2Dcoordinate;

@end

实现

@implementationMapOverlay

-(CLLocationCoordinate2D)coordinate {

//Image center point

returnCLLocationCoordinate2DMake(48.85883,2.2945);

}

- (MKMapRect)boundingMapRect

{

//Latitue and longitude for each corner point

MKMapPointupperLeft=MKMapPointForCoordinate(CLLocationCoordinate2DMake(48.85995,2.2933));

MKMapPointupperRight=MKMapPointForCoordinate(CLLocationCoordinate2DMake(48.85995,2.2957));

MKMapPointbottomLeft=MKMapPointForCoordinate(CLLocationCoordinate2DMake(48.85758,2.2933));

//Building a map rect that represents the image projection on the map

MKMapRectbounds =MKMapRectMake(upperLeft.x, upperLeft.y,fabs(upperLeft.x- upperRight.x),fabs(upperLeft.y- bottomLeft.y));

returnbounds;

}

@end

MapOverlayView 的自定义代码

@interfaceMapOverlayView :MKOverlayView{

}

@end

实现

@implementation MapOverlayView

- (void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)ctx

{

UIImage *image          = [[UIImage imageNamed:@"3.png"] retain];

CGImageRef imageReference = image.CGImage;

//Loading and setting the image

MKMapRect theMapRect    = [self.overlay boundingMapRect];

CGRect theRect           = [self rectForMapRect:theMapRect];

// We need to flip and reposition the image here

CGContextScaleCTM(ctx, 1.0, -1.0);

CGContextTranslateCTM(ctx, 0.0, -theRect.size.height);

//drawing the image to the context

CGContextDrawImage(ctx, theRect, imageReference);

[image release];

}

@end

你可能感兴趣的:(自定义MKOverlayView)