8

I have created Custom Annotation with following:

-(MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation 
{
    MKPinAnnotationView *view = nil;
    if (annotation != mapView.userLocation)
    {
        view = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"myAnnotationIdentifier"];
        if (!view) 
            view = [[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"myAnnotationIdentifier"];

        if (((CustomAnnotation *)annotation).annotationType == 1)
        {
            view.image = [UIImage imageNamed:@"type1.png"];
            view.rightCalloutAccessoryView = nil;
            view.canShowCallout = YES;
        }
        else
        {
            view.image = [UIImage imageNamed:@"type2.png"];
            view.rightCalloutAccessoryView = nil;
            view.canShowCallout = YES;
        }
    }
return view;
}

Problem: When User press and hold for 2 seconds on any Annotation Image (type1 or type2), Image gets replaced by Red PushPin(Default for iPhone MKPinAnnotationView).

I want to avoid this replacement. How can I do so?

Krishna
  • 368
  • 5
  • 17

2 Answers2

23

Instead of declaring and creating an MKPinAnnotationView, declare and create a plain MKAnnotationView.

The MKPinAnnotationView likes to default to the red pin which is what it's for.

  • I also wanted to know, Whether I can put RedPushPin on MKAnnotationView or not? – Krishna Feb 14 '12 at 12:18
  • 1
    Since you are using your own image for the annotation view, you should use MKAnnotationView. However, yes, you can have some annotations that are MKAnnotationView and some that are MKPinAnnotationView (don't set image on those). Use a separate reuse id for each class. –  Feb 14 '12 at 12:31
2

Use didDeselectAnnotationView and didSelectAnnotationView and reselect the image as you did by :-

view.image = [UIImage imageNamed:@"type2.png"];
P.J
  • 6,547
  • 9
  • 44
  • 74
ashraf
  • 21
  • 1