4

I am trying to add a custom image instead of the regular pin on the map. But it remains a red pin... What am I missing?

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{  
    static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
    MKPinAnnotationView *annView = [[MKPinAnnotationView alloc]     initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
    annView.animatesDrop = TRUE;
    annView.image = [UIImage imageNamed:@"CustomPin.png"];
    return annView;
}
Luda
  • 7,282
  • 12
  • 79
  • 139
  • See http://stackoverflow.com/questions/10501341/mkpinannotationview-custom-image-is-replaced-by-pin-with-animating-drop –  Aug 06 '12 at 14:16
  • Your code worked, but the animatesDrop is missing. I added pinView.animatesDrop = YES; but there is an error - property animatesDrop not found on MKAnnotationView. So that fixed it : http://stackoverflow.com/a/2087253/1341180 – Luda Aug 06 '12 at 15:21
  • If you would put this link as an answer, I will check it as correct – Luda Aug 06 '12 at 15:22
  • Yes, as the linked answer says, need to use MKAnnotationView but it doesn't have animatesDrop property. There's another answer linked in that answer that explains a workaround. Feel free to answer this question yourself with the corrections you made. –  Aug 06 '12 at 15:25
  • I have updated the comment- the solution for missing drop functionality found above. – Luda Aug 06 '12 at 15:27

3 Answers3

6

MKMapView: Instead of Annotation Pin, a custom view

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation 
{
    MKAnnotationView *pinView = nil; 
    if(annotation != mapView.userLocation) 
    {
        static NSString *defaultPinID = @"com.invasivecode.pin";
        pinView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
        if ( pinView == nil ) 
            pinView = [[MKAnnotationView alloc]
                                         initWithAnnotation:annotation reuseIdentifier:defaultPinID];

        //pinView.pinColor = MKPinAnnotationColorGreen; 
        pinView.canShowCallout = YES;
        //pinView.animatesDrop = YES;
        pinView.image = [UIImage imageNamed:@"pinks.jpg"];    //as suggested by Squatch
    } 
    else {
        [mapView.userLocation setTitle:@"I am here"];
    }
    return pinView;
}
Community
  • 1
  • 1
DJPlayer
  • 3,244
  • 2
  • 33
  • 40
4

Hi just remove one line from your code... annView.animatesDrop = TRUE;

Remain Code-

-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{  
    static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
    MKPinAnnotationView *annView = [[MKPinAnnotationView alloc]     initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
    annView.image = [UIImage imageNamed:@"CustomPin.png"];
    return annView;
}  
Rajneesh071
  • 30,846
  • 15
  • 61
  • 74
0

I found it helpful to look at Apples Documentation and download the sample code.

http://developer.apple.com/library/ios/#samplecode/MapCallouts/Introduction/Intro.html#//apple_ref/doc/uid/DTS40009746

They are implementing a custom annotation for their map.

btype
  • 1,593
  • 19
  • 32