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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:20:53+00:00 2026-06-10T20:20:53+00:00

Is there a way to change MKAnnotationView style (like from red label with number

  • 0

Is there a way to change MKAnnotationView style (like from red label with number to green colored label with number).

I want to change this style according to distance from target. My annotation is moving, with user.

I dont want to use remove / add annotation, because it causes “blinking”.
Can it be done someway?

UPDATE:

I am adding code, how I am doing it right now

MKAnnotationView *av = [mapView viewForAnnotation:an];

if ([data->type isMemberOfClass:[UserAnnotationImage class]])
{
    UIImage *img = [UIImage imageNamed: ((UserAnnotationImage *)data->type)->url];
    [av setImage:img];
}
else if ([data->type isMemberOfClass:[UserAnnotationLabel class]])
{

    UIView * v = [av viewWithTag:0];
    v = ((UserAnnotationLabel *)data->type)->lbl;

    av.frame = ((UserAnnotationLabel *)data->type)->lbl.frame;
}
else if ([data->type isMemberOfClass:[UserAnnotationView class]])
{

    UIView * v = [av viewWithTag:0];
    v = ((UserAnnotationView *)data->type)->view;
    av.frame = ((UserAnnotationView *)data->type)->view.frame;
}

Sadly, its not working 🙁

  • 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-10T20:20:55+00:00Added an answer on June 10, 2026 at 8:20 pm

    Yes, basically you get a reference to the annotation view and update its contents directly.

    Another way, if you have a custom annotation view class, is to have the annotation view monitor the changes it is interested in (or have something outside tell it) and update itself.

    The first approach is simpler if you are using a plain MKAnnotationView or MKPinAnnotationView.

    Wherever you detect that a change to the view is needed, get a reference to the view by calling the map view’s viewForAnnotation instance method. This is not the same as calling the viewForAnnotation delegate method.

    Once you have a reference to the view, you can modify as needed and the changes should appear immediately.

    An important point is that the logic you use to update the view outside the delegate method and the logic you have in the viewForAnnotation delegate method must match. This is because the delegate method may get called later (after you’ve updated the view manually) by the map view and when it does, the code there should take the updated data into account.

    The best way to do that is to have the annotation view construction code in a common method called both by the delegate method and where you update the view manually.

    See change UIImage from MKAnnotation in the MKMapView for an example that updates just the annotation view’s image.

    For an example (mostly an idea for an approach) of updating the view using a custom annotation view class, see iPad Mapkit – Change title of "Current Location" which updates the view’s pin color periodically (green, purple, red, green, purple, red, etc).


    There are too many unknowns in your code to explain why it doesn’t work. For example:

    • What is data? Is it annotation-specific (is it related to an)? What is type? Does it change after the annotation has been added to the map?
    • Why is data storing entire view objects like a UILabel or UIView instead of just the underlying data that you want to show in those views?
    • imageNamed requires the image to be a resource in the project (not any arbitrary url)
    • Don’t use a tag of 0 (that’s the default for all views). Start numbering from 1.
    • You get a view using viewWithTag but then replace it immediately with another view.

    I’ll instead give a more detailed but simple(r) example…

    Assume you have an annotation class (the one that implements MKAnnotation) with these properties (in addition to coordinate, title, and subtitle):

    @property (nonatomic, assign) BOOL haveImage;
    @property (nonatomic, copy) NSString *labelText;
    @property (nonatomic, copy) NSString *imageName;
    @property (nonatomic, assign) CLLocationDistance distanceFromTarget;
    

    To address the “important point” mentioned above (that the viewForAnnotation delegate method and the view-update-code should use the same logic), we’ll create a method that is passed an annotation view and configures it as needed based on the annotation’s properties. This method will then be called both by the viewForAnnotation delegate method and the code that manually updates the view when the annotation’s properties change.

    In this example, I made it so that the annotation view shows the image if haveImage is YES otherwise it shows the label. Additionally, the label’s background color is based on distanceFromTarget:

    -(void)configureAnnotationView:(MKAnnotationView *)av
    {
        MyAnnotationClass *myAnn = (MyAnnotationClass *)av.annotation;
    
        UILabel *labelView = (UILabel *)[av viewWithTag:1];
    
        if (myAnn.haveImage)
        {
            //show image and remove label...
            av.image = [UIImage imageNamed:myAnn.imageName];
            [labelView removeFromSuperview];
        }
        else
        {
            //remove image and show label...
            av.image = nil;
    
            if (labelView == nil)
            {
                //create and add label...
                labelView = [[[UILabel alloc] 
                    initWithFrame:CGRectMake(0, 0, 50, 30)] autorelease];
                labelView.tag = 1;
                labelView.textColor = [UIColor whiteColor];
                [av addSubview:labelView];
            }
    
            if (myAnn.distanceFromTarget > 100)
                labelView.backgroundColor = [UIColor redColor];
            else
                labelView.backgroundColor = [UIColor greenColor];
    
            labelView.text = myAnn.labelText;
        }
    }
    

    The viewForAnnotation delegate method would look like this:

    -(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation 
    {
        if ([annotation isKindOfClass:[MyAnnotationClass class]])
        {
            static NSString *myAnnId = @"myann";
            MKAnnotationView *av = [mapView dequeueReusableAnnotationViewWithIdentifier:myAnnId];
            if (av == nil)
            {
                av = [[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:myAnnId] autorelease];
            }
            else
            {
                av.annotation = annotation;
            }
    
            [self configureAnnotationView:av];
    
            return av;
        }
    
        return nil;
    }
    

    Finally, the place where the annotation’s properties change and where you want to update the annotation view, the code would look something like this:

    ann.coordinate = someNewCoordinate;
    ann.distanceFromTarget = theDistanceFromTarget;
    ann.labelText = someNewText;
    ann.haveImage = YES or NO;
    ann.imageName = someImageName;
    
    MKAnnotationView *av = [mapView viewForAnnotation:ann];
    [self configureAnnotationView:av];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to change the style of checkboxes when the ItemsOptionListType=CheckList inside
is there any way to change the status of the vm role instances from
Is there any way to change the autonumber field to a number datatype, when
Is there any way to change Visual Studio Auto formatting options? Like VS by
Is there any way to change style of text, format the text inside a
Is there a way to change the css style(defined in the page source) dynamically
is there any way to change the outlook(appearance shape) of UINotification Alert, like I
Is there a way to change the height of a progress view bar in
Is there a way to change the 'Appearance color' (or theme) in the editor
Is there a way to change tabs order in Vim (i.e. change the position

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.