自定義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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容