我创建了一个我添加到MKMapView的注释.如何使用自定义图像而不是标准的红色针?
- @interface AddressAnnotation : NSObject<MKAnnotation> {
- CLLocationCoordinate2D coordinate;
- NSString *title;
- NSString *subtitle;
- MKPinAnnotationColor pinColor;
- }
- @property (nonatomic,retain) NSString *title;
- @property (nonatomic,retain) NSString *subtitle;
- @property (nonatomic,assign) MKPinAnnotationColor pinColor;
- @end
解决方法
MKMapView从其委托方法获取其引脚视图
mapView:viewForAnnotation:所以你必须:
>将视图控制器设置为地图的代表.
>实现mapView:viewForAnnotation:在你的控制器中.
将控制器设置为委托
- @interface MapViewController : UIViewController <MKMapViewDelegate>
使用委托协议标记接口.这就让我们将控制器设置为Interface Builder(IB)中的MKMapView代理.打开包含地图的.xib文件,右键单击MKMapView,然后将代理插槽拖到控制器上.
如果你喜欢使用代码代替IB,添加self.yourMapView.delegate = self;在控制器的viewDidLoad方法中.结果将是一样的.
实现mapView:viewForAnnotation:
- - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
- {
- // this part is boilerplate code used to create or reuse a pin annotation
- static NSString *viewId = @"MKPinAnnotationView";
- MKPinAnnotationView *annotationView = (MKPinAnnotationView*)
- [self.mapView dequeueReusableAnnotationViewWithIdentifier:viewId];
- if (annotationView == nil) {
- annotationView = [[[MKPinAnnotationView alloc]
- initWithAnnotation:annotation reuseIdentifier:viewId] autorelease];
- }
- // set your custom image
- annotationView.image = [UIImage imageNamed:@"emoji-ghost.png"];
- return annotationView;
- }