Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8035203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:14:38+00:00 2026-06-05T02:14:38+00:00

I use plist file to store annotation data that have Name, Address, Coordinates and

  • 0

I use plist file to store annotation data that have Name, Address, Coordinates and Icon (pin image name) strings in dictionary. I need to show my annotations on map with pin image depending on Icon string in plist. I loop my annotation dictionaries but it show on map pin image from first dict on all my pins.

My code:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{


    if ([annotation isKindOfClass:[MKUserLocation class]])
        return nil;

    static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
    MKPinAnnotationView* pinView = [[MKPinAnnotationView alloc]
                                    initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];


    NSString *path = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];

    for(path in dict){

        NSString *theCategory;

        theCategory = [NSString stringWithFormat:@"%@", path];
        NSLog(@"%@", path);

        NSArray *anns = [dict objectForKey:theCategory];

        pinView.image = [UIImage imageNamed:[[anns objectAtIndex:0] objectForKey:@"Icon"]];

    }

    pinView.canShowCallout=YES;


    return pinView;
}

My plist file construction:

enter image description here

What it show to me:

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T02:14:40+00:00Added an answer on June 5, 2026 at 2:14 am

    The viewForAnnotation delegate method will get called for each annotation added.

    The for-loop you have inside that method will run the same way for each annotation. All the for-loop ends up doing (every time for each annotation) is setting pinView.image to the last item read by the for-loop. This happens to be the first item in the first dictionary in the plist.

    You need to instead set pinView.image to the Icon of the item that is for the current annotation that viewForAnnotation is being called for (ie. the annotation parameter that is passed). So you could keep the for-loop and check if the item matches annotation and only then set pinView.image (and then break out of the for-loop).

    But it’s not a good idea to constantly be re-reading and looping through a plist in that delegate method. It’s better to make Icon a property of your annotation class, set the property when creating the annotation (you are probably looping through the plist to create the annotations in the first place), and then just use that property directly from the annotation object itself in the viewForAnnotation delegate method.

    Assuming you have some custom annotation class, add Icon as a property:

    @interface MyAnnotationClass : NSObject<MKAnnotation>
    
    @property (nonatomic, assign) CLLocationCoordinate2D coordinate;
    @property (nonatomic, copy) NSString *title;
    @property (nonatomic, copy) NSString *subtitle;
    @property (nonatomic, copy) NSString *icon;  //<---and @synthesize it in .m
    

    Then in the place where you loop through the plist to create the annotations, set the icon property just like you are setting the title property:

    ann.title = [item objectForKey:@"Name"];
    ann.icon = [item objectForKey:@"Icon"];
    

    Finally, in viewForAnnotation, you can read the icon property directly from the annotation. But first, you should check that annotation is of your custom class type (so the user location blue dot is not affected and to be reasonably sure annotation will have the property you’re about to access):

    - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
    {
        if (![annotation isKindOfClass:[MyAnnotationClass class]])
        {
            // Note "!" sign in front of above condition.
            // Return nil (default view) if annotation is 
            // anything but your custom class.
            return nil;
        }
    
        static NSString* AnnotationIdentifier = @"AnnotationIdentifier";
    
        MKAnnotationView *pinView = [mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationIdentifier];
        // Create an MKAnnotationView instead of MKPinAnnotationView
        // because we are setting a custom image.
        // Using MKPinAnnotationView sometimes overrides custom image
        // with the built-in pin view.
        if (pinView == nil)
        {
            pinView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationIdentifier];
            pinView.canShowCallout = YES;
        }
        else
            pinView.annotation = annotation;
    
        MyAnnotationClass *myAnn = (MyAnnotationClass *)annotation;
        pinView.image = [UIImage imageNamed:myAnn.icon];
        // May want to check if myAnn.icon is blank (length == 0)
        // (OR if pinView.image is still nil after setting)
        // and show some default image in that case otherwise
        // annotation will be invisible.
    
        return pinView;
    }
    

    By the way, in your plist file, Test3 has no Icon setting.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I use NSKeyedArchiver is the data that is written a *.plist, I have
Whenever I want to get data from a plist file I use the following
According to this post I can use the encryption/decryption methods to store/retrieve plist file
I have GPS data stored as as .tcx file. This is a xml file
I'm using a plist to store bookmarks as a simple array of strings. I
I'm currently using *.plist files for my iPhone app to store information that is
I use plist to save data as shown below. I need now to delete
I have a custom plist file which contains dictionaries. Each dictionary contains information about
I've created a small class that loads dictionary items from a plist file. The
I was wondering how to use an image I created as an icon. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.