objective-c – MKAnnotation的自定义图像

我创建了一个添加到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;
}

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...