will this code when working show custom pins and logos or just 1 pin with custom logos? please see plist below, only error is Local declaration of mapView hides instance variable.
<plist version="1.0">
<array>
<dict>
<key>stationIdKey</key>
<string>BP</string>
<key>stationNameKey</key>
<string>Atkinson Dam Cabin Village</string>
<key>stationAddressKey</key>
<string>Atkinson Dam Road, Atkinson Dam</string>
<key>stationLatitudeKey</key>
<string>-27.415056</string>
<key>stationLongitudeKey</key>
<string>152.43057</string>
</dict>
<dict>
<key>stationIdKey</key>
<string>Shell</string>
<key>stationNameKey</key>
<string>BP - AYR DCA</string>
<key>stationAddressKey</key>
<string>108 Edwards Street, AYR</string>
<key>stationLatitudeKey</key>
<string>-19.57094107</string>
<key>stationLongitudeKey</key>
<string>147.4025662</string>
</dict>
and the mapViewController.m file
- (void)viewDidload
{
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] pathForResource:@"stations" ofType:@"plist"];
NSArray *anns = [NSArray arrayWithContentsOfFile:path];
NSLog(@"anns=%@",anns);
for(NSMutableDictionary *note in anns) {
float realLatitude = [[note objectForKey:@"stationLatitudeKey"] floatValue];
float realLongitude = [[note objectForKey:@"stationLongitudeKey"] floatValue];
MyAnnotation* myAnnotation = [[MyAnnotation alloc] init];
CLLocationCoordinate2D theCoordinate;
theCoordinate.latitude = realLatitude;
theCoordinate.longitude = realLongitude;
myAnnotation.coordinate = theCoordinate;
myAnnotation.title = [note objectForKey:@"stationNameKey"];
myAnnotation.subtitle = [note objectForKey:@"stationAddressKey"];
myAnnotation.stationIdKey = [note objectForKey:@"stationIdKey"];
[mapView setDelegate:self];
[mapView addAnnotation:myAnnotation];
[myAnnotation release];
} }
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{ if ([annotation isKindOfClass:[MyAnnotation class]])
{
static NSString *reuseId = @"customAnn";
MKAnnotationView *customAnnotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:reuseId];
if (customAnnotationView == nil)
{
customAnnotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseId] autorelease];
UIImage *pinImage = [UIImage imageNamed:@"pin-green.png"];
[customAnnotationView setImage:pinImage];
customAnnotationView.canShowCallout = YES;
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
customAnnotationView.rightCalloutAccessoryView = rightButton;
}
NSString *iconFilename = @"";
MyAnnotation *myAnn = (MyAnnotation *)annotation;
if ([myAnn.stationIdKey isEqualToString:@"BP"])
iconFilename = @"bp-logo.png";
else
if ([myAnn.stationIdKey isEqualToString:@"Caltex"])
iconFilename = @"caltex.png";
else
if ([myAnn.stationIdKey isEqualToString:@"Shell"])
iconFilename = @"shell.png";
UIImageView *leftIconView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:iconFilename]] autorelease];
customAnnotationView.leftCalloutAccessoryView = leftIconView;
customAnnotationView.annotation = annotation;
return customAnnotationView;
}
return nil; }
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{ if ([view.annotation isKindOfClass:[MyAnnotation class]])
{
MyAnnotation *myAnn = (MyAnnotation *)view.annotation;
NSLog(@"callout button tapped for station id %@", myAnn.stationIdKey);
}
else
{
NSLog(@"callout button tapped for annotation %@", view.annotation);
} }
(Posting a separate answer for the slightly modified question. The original question was how to change the annotation callout image based on some property of the annotation object. The modified question is why the annotations are not appearing at all based on the latest code and how to fix the warning in viewForAnnotation.)
The only obvious problem with the latest code in the question is that the
viewDidLoadmethod is spelled wrong which prevents that code from getting called at all. Please change this:to this:
A few other things to check just in case:
<?xml ...><!DOCTYPE ...><plist ...><array><dict>xxx</dict><dict>yyy</dict><dict>zzz</dict></array></plist>pin-green.png, etc have been added to the project and are named exactly that way (uppercase/lowercase matters on the device)Next, to fix the compiler warning in
viewForAnnotation, change the name of themapViewparameter to something else so it’s different from themapViewivar in the view controller. Following example changes it toaMapView(two places marked with^^^^^^):This is how it looks (your pin and callout images may be different):
If you want the pins themselves to have different images instead of their callouts, then in
viewForAnnotation, callsetImageinstead of setting theleftCalloutAccessoryView.Another minor, unrelated, thing is that you don’t need to call
mapView setDelegateinside the for-loop. You only need to call it once before the for-loop (and if you’ve already connected the map view’s delegate to File’s Owner in the xib, you don’t need to do it in code at all).