Objective c MKAnnotation的自定义图像

Objective c MKAnnotation的自定义图像,objective-c,mapkit,mkannotation,mkannotationview,Objective C,Mapkit,Mkannotation,Mkannotationview,我已经创建了一个注释,并将其添加到MKMapView中。我该如何使用自定义图像而不是标准的红色pin @interface AddressAnnotation : NSObject<MKAnnotation> { CLLocationCoordinate2D coordinate; NSString *title; NSString *subtitle; MKPinAnnotationColor pinColor; } @property (nonat

我已经创建了一个注释,并将其添加到MKMapView中。我该如何使用自定义图像而不是标准的红色pin

@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
@接口地址注释:NSObject{
CLLocationCoordinate2D坐标;
NSString*标题;
NSString*副标题;
MKPinAnnotationColor pinColor;
}
@属性(非原子,保留)NSString*标题;
@属性(非原子,保留)NSString*副标题;
@属性(非原子,赋值)MKPinAnnotationColor pinColor;
@结束

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

MKMapView从其委托方法获取其pin视图,因此您必须:

  • 将视图控制器设置为贴图的代理
  • 在控制器中实现mapView:viewForAnnotation:
  • 将控制器设置为代理

    @interface MapViewController : UIViewController <MKMapViewDelegate>
    

    要设置自定义图像而不是标准的
    MKPinAnnotationView
    唯一的方法是将
    MKAnnotationView
    与函数
    -(MKAnnotationView*)映射视图:(MKMapView*)映射视图用于注释:(id)注释
    。下面是一个例子:

    - (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;
    }
    
    -(可为空的MKAnnotationView*)映射视图:(MKMapView*)映射视图注释:(id)注释{
    if([annotation isKindOfClass:[MKUserLocation类]]){
    返回零;
    }
    静态NSString*标识符=@“注释”;
    MKAnnotationView*aView=[mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    如果(!aView){
    aView=[[MKAnnotationView alloc]initWithAnnotation:annotation重用标识符:标识符];
    aView.image=[UIImage ImageName:@“Untitled1.png”];
    aView.canShowCallout=是;
    aView.draggable=是;
    }否则{
    aView.annotation=注释;
    }
    回位销;
    }
    

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

    您能给我举个例子说明如何处理它吗?我对目标c不熟悉。谢谢
    - (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;
    }