MKMapView 自定义大头针的图片

创建一个MKAnnotationView的子类,用于修改大头针的图片

#import <MapKit/MapKit.h>
#import <UIKit/UIKit.h>

@interface CustomAnnotationView : MKAnnotationView

@end
1 - (id)initWithAnnotation:(id<MKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier {
2     self = [super initWithAnnotation:annotation reuseIdentifier:@"ID"];
3     if (self) {
4         self.backgroundColor = [UIColor clearColor];
5     }
6     
7     return self;
8 }

在mapview的委托函数中,直接使用这个view初始化大头针

 1 - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
 2     //判断系统大头针
 3     if ([annotation isKindOfClass:[mapView.userLocation class]]) {
 4         return nil;
 5     }
 6     
 7     CustomAnnotationView *pinView = (CustomAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"ID"];
 8     if (pinView == nil) {
 9         pinView = [[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"ID"];
10         [pinView setDraggable:YES];
11     }
12     
13     if ([[annotation title] isEqualToString:@"酒店1"]) {
14         pinView.image = [UIImage imageNamed:@"1.PNG"];
15     } else if ([[annotation title] isEqualToString:@"酒店2"]) {
16         pinView.image = [UIImage imageNamed:@"2.png"];
17     } else if ([[annotation title] isEqualToString:@"酒店3"]) {
18         pinView.image = [UIImage imageNamed:@"3.png"];
19     } else {
20         pinView.image = [UIImage imageNamed:@"4.png"];
21     }
22 
23     pinView.canShowCallout = YES;
24     
25     return pinView;
26 }

转载于:http://ningmengjiabing.blog.163.com/blog/static/204847198201292963224732/,有修改

原文地址:https://www.cnblogs.com/xiaochaozi/p/3685971.html