6

在我的 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;
}

在此处输入图像描述

有任何想法吗?

4

4 回答 4

12

如果您想在MKAnnotationView苹果“Pin”上使用不同的图像,则必须使用 aMKAnnotationView而不是 a MKPinAnnotationView

MKPinAnnotationView 不打算以这种方式自定义,并且会不时显示 Pin。类参考仅显示pinColor并且animatesDrop应该更改。

但是,您将丢失 PinAnnotationView 的动画和阴影。

于 2013-02-21T10:15:12.210 回答
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;

我之前已经实现了这一点。所以试试吧。

于 2013-02-21T12:29:26.587 回答
1

使用这个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"];
    }
}

whereMyAnnotation实现如下

typedef enum AnnotationType {
    AnnotationTypeStart,
    AnnotationTypeEnd,
    AnnotationTypeWayPoint,
} AnnotationType;

@interface MyAnnotation : NSObject <MKAnnotation> {
    CLLocationCoordinate2D coordinate;
    NSString *title;
    NSString *subtitle;
    AnnotationType ann;
}
于 2013-02-21T11:47:45.127 回答
1

发布我在最近的项目中使用的示例片段。希望这可以帮助。

- (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;    
}
于 2013-02-21T10:30:10.497 回答