Stumbling a bit on this one, and I have exhausted many hours on this one. I looked at several other questions and answers but can't seem to solve this. Right now the code just makes all the annotations the same, which is the image that is last in my array. Basically, I want to change the annotation image for each pin based on an image I have in a dictionary. So, all 60 pins on the map will be a different image. Here is the method where I setup the annotation:
//Remove any existing custom annotations but not the user location blue dot.
for (id<MKAnnotation> annotation in mapView.annotations)
{
if ([annotation isKindOfClass:[MapPoint class]])
{
[mapView removeAnnotation:annotation];
}
}
NSArray *dict = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"FastFood" ofType:@"plist"]];
//Access name value in dictionary, store to array
// NSArray *nameFromPlist = [dict valueForKey:@"name"];
//Loop through the array of places returned from the Google API.
for (int i=0; i<[data count]; i++)
{
int currentIndex = -1;
for (NSDictionary *plistname in [dict valueForKey:@"name"])//nameFromPlist[@"name"])
{
currentIndex++;
NSPredicate *inBothLists=[NSPredicate predicateWithFormat:@"Self CONTAINS[c] %@", plistname];
BOOL result = [inBothLists evaluateWithObject:[[data objectAtIndex:i] objectForKey:@"name"]];
if (result == YES)
{
//Retrieve the NSDictionary object in each index of the array.
NSDictionary* place = [data objectAtIndex:i];
NSDictionary* image = [dict objectAtIndex:currentIndex];
//There is a specific NSDictionary object that gives us location info.
NSDictionary *geo = [place objectForKey:@"geometry"];
imgRestLogo = nil;
imgRestLogo = [UIImage imageNamed:[image objectForKey:@"image"]];
// NSString *imgtest= [image objectForKey:@"image"];
// NSLog(@"Image Filename is: %@ " , imgtest);
//Get our name and address info for adding to a pin.
NSString *name=[place objectForKey:@"name"];
NSString *vicinity=[place objectForKey:@"vicinity"];
//Get the lat and long for the location.
NSDictionary *loc = [geo objectForKey:@"location"];
//Create a special variable to hold this coordinate info.
CLLocationCoordinate2D placeCoord;
//Set the lat and long.
placeCoord.latitude=[[loc objectForKey:@"lat"] doubleValue];
placeCoord.longitude=[[loc objectForKey:@"lng"] doubleValue];
//Create a new annotiation.
MapPoint *placeObject = [[MapPoint alloc] initWithName:name address:vicinity coordinate:placeCoord];
[mapView addAnnotation:placeObject];
}
This is my viewForAnnotation method:
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
//Define our reuse indentifier.
static NSString *identifier = @"MapPoint";
if ([annotation isKindOfClass:[MapPoint class]]) {
MKAnnotationView *annotationView = (MKAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil) {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
} else {
annotationView.annotation = annotation;
}
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
UIImage *image = imgRestLogo; //[UIImage imageNamed:@"GMPin.png"];
annotationView.image = image;
return annotationView;
}
return nil;
}
Thanks in advance!