【问题标题】:Custom image for MKAnnotationMKAnnotation 的自定义图像
【发布时间】:2011-11-19 15:48:57
【问题描述】:

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

【问题讨论】:

    标签: objective-c mapkit mkannotation mkannotationview


    【解决方案1】:

    MKMapView 从它的委托方法mapView:viewForAnnotation: 获取它的 pin 视图所以你必须:

    1. 将您的视图控制器设置为地图的委托。
    2. 在您的控制器中实现 mapView:viewForAnnotation:。

    将您的控制器设置为委托

    @interface MapViewController : UIViewController <MKMapViewDelegate>
    

    使用委托协议标记接口。这让我们在 Interface Builder (IB) 中将控制器设置为 MKMapView 的委托。打开包含您的地图的 .xib 文件,右键单击 MKMapView,然后将 delegate 出口拖到您的控制器。
    如果您更喜欢使用代码而不是 IB,请在控制器的 viewDidLoad 方法中添加 self.yourMapView.delegate=self;。结果是一样的。

    实现 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;
    }
    

    【讨论】:

      【解决方案2】:

      覆盖mapView:viewForAnnotation: 委托方法。如果 annotation 参数指向您的自定义注释之一,则返回您喜欢的自定义视图。

      【讨论】:

      • 你能给我举个例子来说明如何做到这一点吗?我是目标 c 的新手。谢谢
      【解决方案3】:

      要设置自定义图像而不是标准MKPinAnnotationView,唯一的方法是将MKAnnotationView 与函数- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id &lt;MKAnnotation&gt;)annotation 一起使用。示例如下:

      - (nullable MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
      
           if ([annotation isKindOfClass:[MKUserLocation class]]) {
                      return  nil;
           }
      
           static NSString *identifier = @"Annotation";
      
           MKAnnotationView *aView = [mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
      
           if (!aView) {
                aView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
                aView.image = [UIImage imageNamed:@"Untitled1.png"];
                aView.canShowCallout = YES;
                aView.draggable = YES;
           } else {
                aView.annotation = annotation;
           }
      
           return pin;
      }
      

      对于aView.image 值,您可以设置自己的图像。并查看MKAnnotationView 类引用以更好地处理它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-12-31
        • 2015-08-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多