Shows different contact images in MKMapview

In my MKMap view, I configured the output of the annotation with the image. But still, some contacts are static and do not display this image.

I use - (MKAnnotationView *) mapView: (MKMapView *) mapView viewForNnotation: (id) annotation to set the pin image.

Adding my code and screen here:

-(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;
}

enter image description here

Any ideas?

+5
source share
4 answers

If you want a different image MKAnnotationViewthan apples "Pin", you should use MKAnnotationViewinstead MKPinAnnotationView.

MKPinAnnotationView Pin. , pinColor animatesDrop.

PinAnnotationView.

+11

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;

. .

+2

, . , .

- (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;    
}
+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"];
    }
}

MyAnnotation

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

@interface MyAnnotation : NSObject <MKAnnotation> {
    CLLocationCoordinate2D coordinate;
    NSString *title;
    NSString *subtitle;
    AnnotationType ann;
}
+1

All Articles