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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:44:22+00:00 2026-05-23T08:44:22+00:00

I have a MapView onto which I put some overlays (MKPolygon). I have to

  • 0

I have a MapView onto which I put some overlays (MKPolygon).
I have to maintain groups of MKPolygon.

For this, I have a PolyGonGroupClass that keeps each poly and the color and alpha that should be used to display all these polygons.

So, each time I find a poly, I add it as a map overlay and memorize in into the correct group of polys.
All my groups of poly are kepts into an instance var.

[self.mapView addOverlay:poly];
[thecorrectpolygroup addObject:poly];
...
[self.mypolygroups addObject:thecorrectpolygroup];


@interface PolyGonGroupClass : NSObject {
    UIColor* __color;
    float __alpha;
    NSMutableArray* __polygons;
}

Well…

At this point, how may I tell to the MapView the color and alpha of each poly and update them on the fly when one of these color change ?

I found a - (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay method, but I don’t see how I may use it to retrieve the values I put into the memorised values.

I guess I should use the overlay parameter, but how may I retrieve the correct group of poly that correspond to that overlay ?

  • 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-23T08:44:22+00:00Added an answer on May 23, 2026 at 8:44 am

    In viewForOverlay, you can loop through your mypolygroups array and check if the overlay is in that group’s polygons array:

    - (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
    {
        if ([overlay isKindOfClass:[MKPolygon class]])
        {
            MKPolygonView *pv = [[[MKPolygonView alloc] initWithPolygon:overlay] autorelease];
    
            for (PolyGonGroupClass *pgc in mypolygroups) 
            {
                if ([pgc.polygons containsObject:overlay])
                {
                    pv.fillColor = pgc.color;
                    pv.alpha = pgc.alpha;
                    break;
                }
            }
    
            return pv;
        }
    
        return nil;
    }
    

    However, for this to work right, you’ll have to slightly change where you call addOverlay. In the code you’ve shown, you are calling addOverlay before adding it to the polygons array and before adding the group to the mypolygroups array. This can cause viewForOverlay to fire before the arrays contain data.

    Instead, move just the addOverlay calls to after the mypolygroups array is fully populated. So after adding the last group to mypolygroups:

    ...
    [self.mypolygroups addObject:theLastPolygroup];
    
    for (PolyGonGroupClass *pgc in mypolygroups) 
    {
        for (MKPolygon *p in pgc.polygons) 
        {
            [mapView addOverlay:p];
        }
    }
    

    Finally, to update a group’s color and alpha later after it’s already on the map, you can do something like this:

    //pgc is some instance of PolyGonGroupClass
    pgc.alpha = newAlpha;
    pgc.color = newColor;
    
    for (MKPolygon *p in pgc.polygons) 
    {
        MKPolygonView *pv = (MKPolygonView *)[mapView viewForOverlay:p];
        pv.alpha = pgc.alpha;
        pv.fillColor = pgc.color;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MapView that will contain alot of overlays. For this i've extended
I have a FrameLayout view which contains one (MapView-like) control and some additional buttons
I have a detail view that includes three UIButtons, each of which pushes a
I have a mapview and i put an annotation which shows a custom title
I have a MapView and I’m overlaying 1,700 points onto it, each with the
I have a MapView with Overlays like this: public class MyMapActivity extends MapActivity {
I have a MapView with a toolbar button that when pushed adds overlays to
I have a MapView with some overlays and a subclass of MyLocationOverlay on top
I have set up a MapView Class which has Overlays (and they're working fine.)
I have a mapview with several annotations. Every annotation has a leftCalloutAccessoryView which is

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.