【问题标题】:Shows different Pin images in MKMapview在 MKMapview 中显示不同的 Pin 图
【发布时间】:2013-02-21 09:51:12
【问题描述】:

在我的 MKMap 视图中,我使用图像自定义了注释图钉。但仍有一些图钉是静态的,没有显示给定的图像。

我正在使用 -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id )annotation 来设置 pin 图像。

在此处添加我的代码和屏幕:

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
 {


if ([annotation isKindOfClass:[MKUserLocation class]])
    return nil;

static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
MKPinAnnotationView* pinView = [[MKPinAnnotationView alloc]
                                 initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
pinView.animatesDrop=YES;
pinView.canShowCallout=YES;
    pinView.pinColor= MKPinAnnotationColorGreen;

    pinView.enabled = YES;
    pinView.canShowCallout = YES;
    pinView.image=[UIImage imageNamed:@"bublerest.png"]; //here I am giving the image  





UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton setTitle:annotation.title forState:UIControlStateNormal];
[rightButton addTarget:self
                action:@selector(showDetails:)
      forControlEvents:UIControlEventTouchUpInside];

    pinView.rightCalloutAccessoryView = rightButton;

UIImageView *profileIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"rest_image2.png"]];
pinView.leftCalloutAccessoryView = profileIconView;


return pinView;
}

有什么想法吗?

【问题讨论】:

  • 您能看到任何图钉的自定义地图图标,还是只有一些图钉没有显示?

标签: iphone ios map mkmapview mkannotation


【解决方案1】:

如果您希望MKAnnotationView 上的图像与苹果“Pin”不同,则必须使用MKAnnotationView 而不是MKPinAnnotationView

MKPinAnnotationView 不打算以这种方式自定义,并且会不时显示 Pin。 Class Reference 表明只有 pinColoranimatesDrop 应该被更改。

不过,您将丢失 PinAnnotationView 的动画和阴影。

【讨论】:

  • 虽然这里的其他答案都提供了自定义图像的方法,但只有这个真正说明了问题所在以及如何解决它。
  • 我在 iOS9 中遇到了这个问题(Apple pin 图像而不是我的 pin 图像)。在您的建议现在可以正常工作之后!谢谢
  • 如果您希望 Apple pin (iPin?) 和您自己的自定义图像显示在同一个地图上 - 使用自定义注释并添加一个指示符以指示所需的 annotationView 的“类型”(常规或别针)。然后根据类型出列一个注解视图或者pinAnnotationView,分别处理这两种情况。
【解决方案2】:

MKAnnotation 协议中创建一个属性,用于设置引脚,例如

@interface AddressAnnotation1 : NSObject<MKAnnotation> {
}
@property (nonatomic, retain) NSString *mPinColor;

在.m文件实现文件中

- (NSString *)pincolor{
    return mPinColor;
}

- (void) setpincolor:(NSString*) String1{
    mPinColor = String1;
}

添加annotation 时,同时设置pin 颜色。

#pragma mark annotation delegate
- (MKAnnotationView *) mapView:(MKMapView *)mapView1 viewForAnnotation:(AddressAnnotation1 *) annotation
{
    UIImage *anImage = nil;

    MKAnnotationView *annView=(MKAnnotationView*)[mapView1 dequeueReusableAnnotationViewWithIdentifier:@"annotation"];
    if(annView==nil){
        annView=[[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"annotation"] autorelease];
    }
    if([annotation.mPinColor isEqualToString:@"green"])
    {
        anImage=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Google map pin 02.png" ofType:nil]];
    }
    else if([annotation.mPinColor isEqualToString:@"red"])
    {
        anImage=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Google map pin 01.png" ofType:nil]];
    }
    else if([annotation.mPinColor isEqualToString:@"blue"])
    {
        anImage=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Google map pin 02.png" ofType:nil]];
    }
    annView.image = anImage;
return annView;
}

如果您不想为引脚自定义图像,请将条件语句的主体替换为

annView.pinColor = MKPinAnnotationColorGreen;

annView.pinColor = MKPinAnnotationColorRed;

annView.pinColor = MKPinAnnotationColorPurple;

我之前已经实现过。那就试试吧。

【讨论】:

  • 这段代码应该可以正常工作,但在我的项目中它不工作。我还将 KML 加载到地图中。
【解决方案3】:

发布一个我在最近的项目中使用的示例 sn-p。希望这会有所帮助。

- (MKAnnotationView *) mapView:(MKMapView *)mapView1 viewForAnnotation:(id <MKAnnotation>) annotation{

Annotation *annotationInst = (Annotation*)annotation;

MKAnnotationView *pinView = nil;
if(annotation != mapView.userLocation)
{
    static NSString *defaultPinID = @"pinId";
    pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];

    if ( pinView == nil ){
        pinView = [[[MKAnnotationView alloc]
                   initWithAnnotation:annotation reuseIdentifier:defaultPinID]autorelease];           
    }


    pinView.canShowCallout = YES;
    pinView.image = [UIImage imageNamed:LOCATION_BUBBLE];

    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    pinView.rightCalloutAccessoryView = rightButton;
}    
return pinView;    
}

【讨论】:

    【解决方案4】:

    viewForAnnotation中使用它

    if ([annotation isKindOfClass:[MyAnnotation class]]) {
        // try to dequeue an existing pin view first
        static NSString* myAnnotationIdentifier = @"MyAnnotationIdentifier";
    
        // If an existing pin view was not available, create one
        MKPinAnnotationView* customPinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:myAnnotationIdentifier];
    
        if ([(MyAnnotation *)annotation ann] == AnnotationTypeStart) {
            NSLog(@"In Start");
            customPinView.enabled = NO;
            customPinView.image = [UIImage imageNamed:@"begin.png"];
        }
    }
    

    MyAnnotation 的实现方式如下

    typedef enum AnnotationType {
        AnnotationTypeStart,
        AnnotationTypeEnd,
        AnnotationTypeWayPoint,
    } AnnotationType;
    
    @interface MyAnnotation : NSObject <MKAnnotation> {
        CLLocationCoordinate2D coordinate;
        NSString *title;
        NSString *subtitle;
        AnnotationType ann;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多