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 841823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:51:45+00:00 2026-05-15T05:51:45+00:00

I’m trying setup a map that displays different pin colors depending on the type/class

  • 0

I’m trying setup a map that displays different pin colors depending on the type/class of the location in question. I know this is a pretty common thing to do, but I’m having trouble getting the viewForAnnotation delegate to consistently update/set the pin color.
I have a showThisLocation function that basically cycles through a list of AddressAnnotations and then based on the annotation class (bus stop, hospital, etc.) I set an


if( myClass == 1){
 [defaults setObject:@"1" forKey:@"currPinColor"];
 [defaults synchronize];
 NSLog(@"Should be %@!", [defaults objectForKey:@"currPinColor"]);
} else if( myClass ==2 ){
 [defaults setObject:@"2" forKey:@"currPinColor"];
 [defaults synchronize];
 NSLog(@"Should be %@!", [defaults objectForKey:@"currPinColor"]);
}
[_mapView addAnnotation:myCurrentAnnotation];

then my viewForAnnotation delegate looks like this,


- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
    if( annotation == mapView.userLocation ){ return nil; }

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    MKPinAnnotationView *annView = nil;
    annView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"currentloc"];
    if( annView == nil ){
        annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
    }

    annView.pinColor =  [defaults integerForKey:@"currPinColor"];
    NSLog(@"Pin color: %d", [defaults integerForKey:@"currPinColor"]);
    annView.animatesDrop=TRUE;
    annView.canShowCallout = YES;
    annView.calloutOffset = CGPointMake(-5, 5);
    return annView;
}

The problem is that, although the NSLog statements in the “if” block always confirm that the color has been set, the delegate sometimes but not always ends up with the correct color. I’ve also noticed that what generally happens is that the first search for a new location will set all pins to the last color in the “if” block, but search for the same location again will set the pins to the correct color.

I suspect I am not supposed to usen NSUserDefaults in this way, but I also tried to create my own subclass for MKAnnotation which included an additional property “currentPinColor”, and while this allowed me to set the “currentPinColor”, when I tried to access the “currentPinColor from the delegate method, the compiler complained that it didn’t know anything about “currentPinColor in connection with MKAnnotation. Fair enough I guess, but then I tried to revise the delegate method,


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

instead of the default


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

at which point the compiler complained that it didn’t know anything about the protocol for MyCustomMKAnnotation in this delegate context.

What is the proper way to set the delegate method and/or MyCustomMKAnnotation, or what is the appropriate way to achieve consistent pinColor settings. I’m just about out of ideas for things to try 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-05-15T05:51:45+00:00Added an answer on May 15, 2026 at 5:51 am

    I solved my own problem. Sometimes just formulating the question is enough!
    The issue was the way I was trying to access my MKAnnotation delegate.

    After posting I happened on these two related posts,

    MKPinAnnotationView: Are there more than three colors available?

    Mapkit issue in finding annotation current position

    The answer to the first post, by user Cannonade, suggests

    
    - (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
    {   
        // ... get the annotation delegate and allocate the MKAnnotationView (annView)
        if ([annotationDelegate.type localizedCaseInsensitiveCompare:@"NeedsBluePin"] == NSOrderedSame)
        {
            UIImage * image = [UIImage imageNamed:@"blue_pin.png"];
            UIImageView *imageView = [[[UIImageView alloc] initWithImage:image] autorelease];
            [annView addSubview:imageView];
        }
        // ...
    

    however my issue was that I didn’t know how to the get the delegate and it was floating around somewhere in the … ellipsis part of the above snippet. The answer to the second post, also by user Cannonade, filled this missing bit in,

    
    - (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
    {
        AnnotationDelegate * delegate = (AnnotationDelegate*)view.annotation;
        // do stuff with delegate.position;
    }
    

    Putting these two snippets together produced the desired result and finally solved my problem.

    The working viewForAnnotation delegate method then looks like,

    
    - (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
        if( annotation == mapView.userLocation ){ return nil; }
        AddressAnnotation *delegate = annotation;  //THIS CAST WAS WHAT WAS MISSING!
        MKPinAnnotationView *annView = nil;
        annView = (MKPinAnnotationView*)[mapView dequeueReusableAnnotationViewWithIdentifier:@"currentloc"];
        if( annView == nil ){
            annView = [[MKPinAnnotationView alloc] initWithAnnotation:delegate reuseIdentifier:@"currentloc"];
        }
    
        if ([delegate.classPinColor localizedCaseInsensitiveCompare:@"green"] == NSOrderedSame) {
            annView.pinColor = MKPinAnnotationColorGreen;
        }else if ([delegate.classPinColor localizedCaseInsensitiveCompare:@"purple"] == NSOrderedSame){
            annView.pinColor = MKPinAnnotationColorPurple;
        }else if ([delegate.classPinColor localizedCaseInsensitiveCompare:@"red"] == NSOrderedSame){
            annView.pinColor = MKPinAnnotationColorRed;
        }
        annView.animatesDrop=TRUE;
        annView.canShowCallout = YES;
        annView.calloutOffset = CGPointMake(-5, 5);
        return annView;
    }
    

    Hopefully this will be of help to someone else.

    Thanks Cannonade!

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

Sidebar

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.