3

I would change the color from red to green of an annotation when the user pin tapped addition to changing its title and subtitle.

I am truly lost. I searched how to make a custom annotation pin, ok. I found the implementation of the method when the user touches the pin didSelectAnnotationView and it works when I tap the annotation NSLog(@"Tap") ; , but now I can not change the pin that was touched.

Thank you very much everyone for your contributions.

Ciao

Mouna Cheikhna
  • 35,154
  • 10
  • 46
  • 68
Antonio
  • 33
  • 1
  • 4

3 Answers3

5

To set the pin color, make use of MKPinAnnotationView pinColor property.

MKPinAnnotationView *pin = [[MKPinAnnotationView alloc] init]
pin.pinColor = MKPinAnnotationColorGreen;

For custom annotation image, set the image property, as such.

UIImage *annImage = [UIImage imageNamed:@"AnnotationIcon.png"];
annView.image = annImage;

Do note that the MKPinAnnotationView animateDrop property will not work on custom images. There's a way to duplicate that animation though. See How do I animate MKAnnotationView drop?

Update So bascially, you do this if you wanna change from red to green upon being selected.

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKPinAnnotationView *)view {
    view.pinColor = MKPinAnnotationColorGreen;

}

- (MKAnnotationView *)mapView:(MKMapView *)aMapView
            viewForAnnotation:(id)ann {

    NSString *identifier = @"myPin";
    MKPinAnnotationView *annView = (MKPinAnnotationView *)
    [aMapView dequeueReusableAnnotationViewWithIdentifier:identifier];
    if (annView == nil) {
        annView= [[[MKPinAnnotationView alloc] initWithAnnotation:ann
                                               reuseIdentifier:identifier]
               autorelease];
    } else {
        annView.annotation = ann;
    }
// you can define the properties here.

return annView;
}
Community
  • 1
  • 1
Gavin
  • 2,484
  • 5
  • 33
  • 68
  • This is my code, but don't works- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view { view.annotation.pinColor = MKPinAnnotationColorGreen; – Antonio Aug 18 '11 at 16:47
  • Xcode tells me: error: Semantic Issue: Property 'pinColor' not found on object of type 'id' – Antonio Aug 18 '11 at 16:55
  • 2
    pinColor is property of MKPinAnnotationView object not MKAnnotationView. – Gavin Aug 18 '11 at 17:13
  • Oh, sorry, I saw it but I don't know how do it. I'm newbie, and my english is very bad. I have this method declared: - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view { NSLog(@"Annotation Tap"); } It's Ok, or this? - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKPinAnnotationView *)view { NSLog(@"Annotation Tap"); Sorry, Antonio } – Antonio Aug 18 '11 at 18:25
  • Sorry again. Now I can see your edited answer. Let me see and I'll tell you something. Ciao. Antonio – Antonio Aug 18 '11 at 18:28
  • I've create a new project to test your answer, and it works!!!!. Now I'm going to test it in the project that I'm working. Million thanks for your patience and your contributions. Now "Life is beautiful". Ciao. Antonio. – Antonio Aug 18 '11 at 18:37
  • Ok. It works!!!. Finally, I'm trying to change the title viewPin.annotation.title = @"KKKKKKk"; , but I have an error: Semantic Issue: Setter method is needed to assign to object using property assignment syntax. Thanks a lot. Antonio – Antonio Aug 18 '11 at 19:01
  • 1
    Why are you doing "viewPin.annotation.title =@"xxx" " Should be just "view.title =@"xxx" " Make sure you created a MKPinAnnotationView and not MKAnnotationView at viewForAnnotation method like what I posted. It's okay I am a newbie too, just that I had been playing around with mapkit for quite a while. Your english is fine too. – Gavin Aug 19 '11 at 00:49
4

In your method set the pinColor property of your MKAnnotationView as follows:

annotationView.pinColor = MKPinAnnotationColorRed; // Green or Purple
Faizan S.
  • 8,547
  • 8
  • 32
  • 61
  • This is my code, but don't works - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view { view.annotation.pinColor = MKPinAnnotationColorGreen; – Antonio Aug 18 '11 at 16:48
  • Xcode tells me: error: Semantic Issue: Property 'pinColor' not found on object of type 'id' – Antonio Aug 18 '11 at 16:56
  • you have to use the `pinView` on your `MKAnnotationView` - use `view.pinColor = MKPinAnnotationColorGreen` – Faizan S. Aug 18 '11 at 17:53
  • Thak You so much for your contibution. Ciao. Antonio – Antonio Aug 18 '11 at 18:41
1

(re) look this :

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKPinAnnotationView *)view {
    view.pinColor = MKPinAnnotationColorGreen;

}

this is a MKPinAnnotationView (and not MKAnnotationView) in param

kleopatra
  • 49,346
  • 26
  • 88
  • 189