創建一個自定義的註釋類。
在CustomAnnotation.h
#import <MapKit/MapKit.h>
@interface CustomAnnotation : MKAnnotationView
@end
在CustomAnnotation.m
#import "CustomAnnotation.h"
@implementation CustomAnnotation
-(id)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
if (self != nil) {
CGRect frame = self.frame;
frame.size = CGSizeMake(46.0, 49.0);
self.frame = frame;
self.backgroundColor = [UIColor clearColor];
self.centerOffset = CGPointMake(-5, -5);
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
[[UIImage imageNamed:@"IMAGE_NAME"] drawInRect:rect];
}
我們MapView的委託方法
viewForAnnotation
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString *defaultPinId = @"Pin";
CustomAnnotation *pinView = (CustomAnnotation *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinId];
if (pinView == nil) {
pinView = [[CustomAnnotation alloc]initWithAnnotation:annotation reuseIdentifier:defaultPinId];
}
else {
pinView.annotation = annotation;
}
return pinView;
}
不幸的是,不工作 – Noor
如果它不工作,爲什麼它被標記爲正確的答案? – arniotaki
無法正常工作.... –