1

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!

MSU_Bulldog
  • 3,501
  • 5
  • 37
  • 73
hansoac
  • 47
  • 8
  • Why is dict an `NSArray*` ? If it is really an NSArray, why are you calling `valueForKey:` on it. – pedros Jul 29 '14 at 19:09
  • the plist is an array of dictionaries. I am calling valueForKey because I am grabbing the value of "name" for each object. The code works as expected, except adding the annotations. All the pins have the same image, which is the last image in my array. – hansoac Jul 29 '14 at 19:37
  • 1
    You cannot assume that viewForAnnotation is called immediately and once after each addAnnotation is done. You must make and set `imgRestLogo` as a property of the annotation itself. See http://stackoverflow.com/questions/24215210/does-mkannotationview-buffer-its-input-queue, http://stackoverflow.com/questions/19514494/map-annotation-display-all-the-same-image-pins-for-all-points, etc. –  Jul 29 '14 at 19:45
  • Thank you Anna, I am reviewing that post to see if I can figure it out! I may struggle on setting imgRestLogo as a property, but perhaps I will understand from the example. – hansoac Jul 29 '14 at 19:58
  • I am reviewing those two posts as well as other answers you have given in regards to mkannotations, Anna. You are quite the expert. I am still having trouble with this, partly because I am not an objective-c expert. I have one year exposure to it, and am also new to Object Orientation concepts. Could you aid me a bit further in this? I would be very grateful. – hansoac Jul 29 '14 at 20:49

1 Answers1

0

My suggestion is to subclass a MKAnnotation with an imageName property first, assuming you have a class called MYAnnotation



    @interface MYAnnotation : NSObject 
    @property (copy, nonatomic) NSString *title;
    @property (copy, nonatomic) NSString *subtitle;
    @property (copy, nonatomic) NSString *imageName;
    @end

Then add your image name. (Note: The way to get an image name is according to the structure of your plist file.)



    NSDictionary *imageName = [dict objectAtIndex:currentIndex];
    annotation.imageName = imageName;

In the delegte, set image by the following.


    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;

    NSString *imageName = ((MYAnnotation *)annotation).imageName;
    UIImage *image = [UIImage imageNamed:imageName];
    annotationView.image = image;

    return annotationView;
Allen
  • 1,714
  • 17
  • 19