如果上面三種還是不能滿足要求蜜宪,那么就需要自定義了畏腕。這個(gè)例子我是學(xué)習(xí)的別人的代碼。
MapOverlay實(shí)現(xiàn)協(xié)議MKOverlay。
MapOverlayView繼承MKOverlayView鸡捐。
下面是代碼
@interfaceMapOverlay :NSObject {
}
- (MKMapRect)boundingMapRect;
@property(nonatomic,readonly)CLLocationCoordinate2Dcoordinate;
@end
實(shí)現(xiàn)
@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
實(shí)現(xiàn)
@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