Quote:
Originally Posted by iphonig
Hey All,
I am working on an app right now and I have the colors of the pins changing when I want them to, but as I'm sure a lot of you are well aware, Apple limits you to only using red, green, and purple pins. I figured out how to have an image show rather than one of Apple's own colored pins but the problem is I will click the button that makes the custom pin drop and it will work fine. But if I press it again (and yes there are supposed to be multiple pins on the map) red pins show up rather than the custom ones again. The code I wrote out is below. Any ideas?
Code:
- (MKAnnotationView *) mapView:(MKMapView *)mapView
viewForAnnotation:(id <MKAnnotation>) annotation {
MKPinAnnotationView *anView =[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:@"pin"];
if(pinColor == @"red")
anView.pinColor = MKPinAnnotationColorRed;
if(pinColor == @"green")
anView.pinColor = MKPinAnnotationColorGreen;
if(pinColor == @"amber")
anView.image = [UIImage imageNamed:@"amberpin.png"];
anView.animatesDrop = TRUE;
anView.canShowCallout = YES;
return anView;
[anView release];
}
BTW 'annotation' is pointer to a MapAnnotation class I have written.
I add the pins with
Code:
[mapView addAnnotation:annotation];
Again any help would be appreciated!
|
Looking at it more closely, this code has all kinds of problems.
1. You're not calling dequeueReusableAnnotationViewWithIdentifier to check for a recycled annotation view.
2. You're always creating an instance of MKPinAnnotationView. That type only supports one of the standard pin annotations. You need to first decide what kind of annotation view you need, and THEN use the code that dequeues/creates the right kind of annotation view.
3. You should autorelease your annotation view, not release it.
4. You have a release call after the return. That line of code will never get called. You need to put the (autorelease) before the return.
There may be other problems too. I didn't proof it that carefully.