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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:31:06+00:00 2026-05-26T23:31:06+00:00

In my map, I have to show annotations from a kml file via an

  • 0

In my map, I have to show annotations from a kml file via an URL. I also need to show only the annotations inside a polygon or circle area (or both of them if the user has both overlays drawn).

I have seen the question How to determine if an annotation is inside of MKPolygonView (iOS), but I have two perplexities:

  1. Regarding the annotation coordinates, should I use the coordinates of the annotations from the addAnnotation method?
  2. In the mentioned question a new overlay is created, but I have two different overlays created elsewhere. So my question is: what is the most suitable place to put this code (or something like that)?

EDIT: I’ve created some code:

-(IBAction)showKmlData:(id)sender
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"KMLGenerator" ofType:@"kml"];

kml = [[KMLParser parseKMLAtPath:path] retain];

NSArray *annotationsImmut = [kml points];
//[mapview addAnnotations:annotations]; not anymore
NSMutableArray *annotations = [annotationsImmut mutableCopy];
[self filterAnnotations:annotations];

MKMapRect flyTo = MKMapRectNull;

for (id <MKAnnotation> annotation in annotations) {
    MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation.coordinate);
    MKMapRect pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
    if (MKMapRectIsNull(flyTo)) {
        flyTo = pointRect;
    } else {
        flyTo = MKMapRectUnion(flyTo, pointRect);
    }
}

mapview.visibleMapRect = flyTo;
}


-(void)filterAnnotations:(NSMutableArray *)annotationsToFilter {

for (int i=0; i<[annotationsToFilter count]; i++) {

    CLLocationCoordinate2D mapCoordinate = [[annotationsToFilter objectAtIndex:i] coordinate];

    MKMapPoint mapPoint = MKMapPointForCoordinate(mapCoordinate);

    MKPolygonView *polygonView = 
        (MKPolygonView *)[mapView viewForOverlay:polygonOverlay];

    MKCircleView *circleView = 
        (MKCircleView *)[mapView viewForOverlay:circleOverlay];

    CGPoint polygonViewPoint = [polygonView pointForMapPoint:mapPoint];
    CGPoint circleViewPoint = [circleView pointForMapPoint:mapPoint];

    BOOL mapCoordinateIsInPolygon = 
        CGPathContainsPoint(polygonView.path, NULL, polygonViewPoint, NO);

    BOOL mapCoordinateIsInCircle = 
        CGPathContainsPoint(circleView.path, NULL, circleViewPoint, NO);

        if( mapCoordinateIsInPolygon || mapCoordinateIsInCircle )
            [annotationsToFilter removeObjectAtIndex:i];
}
[mapView addAnnotations:annotationsToFilter];
}

EDIT Nr.2 Here is my implementation of viewForOverlay delegate method.I see the overlays, circles and polygons I create.I see all the annotations.ALL of them, those inside and outside the overlays…

-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id)overlay 
{
    MKCircleView* circleView = [[[MKCircleView alloc] initWithOverlay:overlay] autorelease];
circleOverlay = circleView;
    circleView.fillColor = [UIColor blueColor];
    circleView.strokeColor = [UIColor blueColor];
    circleView.lineWidth = 5.0;
    circleView.alpha = 0.20;

    MKPolygonView *polygonView = [[[MKPolygonView alloc] initWithOverlay:overlay] autorelease];
polygonOverlay = polygonView;
    polygonView.fillColor = [UIColor blueColor];
    polygonView.strokeColor = [UIColor blueColor];
    polygonView.lineWidth = 5.0;
    polygonView.alpha = 0.20;


if ([overlay isKindOfClass:[MKCircle class]])
{   
    return circleView;
}

else
    return polygonView;

}

  • 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-26T23:31:07+00:00Added an answer on May 26, 2026 at 11:31 pm

    Overall, that looks fine but there is a problem in the filterAnnotations method with how the annotations are being removed from the annotationsToFilter array.

    What will happen is that some annotations will be skipped and never go through the check.

    For example:

    • Say there are 5 annotations (0=A, 1=B, 2=C, 3=D, 4=E)
    • The for-loop starts at index 0 and “A” meets the condition for removal
    • “A” is removed from the array and now the other annotations are moved down one index so after the removal the array is: (0=B, 1=C, 2=D, 3=E)
    • Now the for-loop goes to the next index which is 1 (so annotation C is checked)
    • So annotation B is skipped and is never checked

    One way to fix this is to collect the annotations you want to keep in another array “annotationsToAdd” instead of removing them from the original and pass annotationsToAdd to the addAnnotations method.

    Below is the suggested modification. I’d also recommend moving the viewForOverlay calls outside the for-loop since those references won’t change during the loop so there’s no need to call them repeatedly.

    -(void)filterAnnotations:(NSMutableArray *)annotationsToFilter 
    {
        NSMutableArray *annotationsToAdd = [NSMutableArray array];
    
        //Get a reference to the overlay views OUTSIDE the for-loop since
        //they will remain constant so there's no need to keep calling
        //viewForOverlay repeatedly...
        MKPolygonView *polygonView = (MKPolygonView *)[mapView viewForOverlay:polygonOverlay];
    
        MKCircleView *circleView = (MKCircleView *)[mapView viewForOverlay:circleOverlay];
    
        for (int i=0; i < [annotationsToFilter count]; i++) 
        {
            //get a handy reference to the annotation at the current index...
            id<MKAnnotation> currentAnnotation = [annotationsToFilter objectAtIndex:i];
    
            CLLocationCoordinate2D mapCoordinate = [currentAnnotation coordinate];
    
            MKMapPoint mapPoint = MKMapPointForCoordinate(mapCoordinate);
    
            CGPoint polygonViewPoint = [polygonView pointForMapPoint:mapPoint];
            CGPoint circleViewPoint = [circleView pointForMapPoint:mapPoint];
    
            BOOL mapCoordinateIsInPolygon = CGPathContainsPoint(polygonView.path, NULL, polygonViewPoint, NO);
    
            BOOL mapCoordinateIsInCircle = CGPathContainsPoint(circleView.path, NULL, circleViewPoint, NO);
    
            if ( !mapCoordinateIsInPolygon && !mapCoordinateIsInCircle )
                //Note the reversed if-condition because now 
                //we are finding annotations we want to KEEP
            {
                [annotationsToAdd addObject:currentAnnotation];
            }
        }
    
        [mapView addAnnotations:annotationsToAdd];
    }
    

    Also, I noticed that in the showKmlData method you are using the variable mapview but in filterAnnotations it’s mapView (uppercase V). Hopefully, the compiler will give you a warning about that.


    Additional Info:
    Based on your comments and the viewForOverlay code you added to your question…

    First, the compiler warning class 'MKPolygonView' does not implement the 'MKOverlay' protocol that you are getting implies that the variables polygonOverlay and circleOverlay are declared as MKPolygonView and MKCircleView instead of MKPolygon and MKCircle.

    Second, the code in the viewForOverlay delegate method is wrong. It tries to create both a circle and polygon view for whatever overlay is passed in and then checks what kind of class the overlay is. It also seems to be saving a reference to the overlay view but the rest of the code assumes that we are keeping a reference to the overlay (the MKOverlay object–not the MKOverlayView).

    Try the following changes…

    //polygonOverlay and circleOverlay should be declared as MKOverlay objects...
    @property (nonatomic, retain) MKPolygon *polygonOverlay;
    @property (nonatomic, retain) MKCircle *circleOverlay;
    
    //save a reference to them when you call addOverlay...
    self.polygonOverlay = [MKPolygon polygonWithCoordinates:polyCoords count:coordsCount];
    [mapView addOverlay:polygonOverlay];
    self.circleOverlay = [MKCircle circleWithCenterCoordinate:cirleCenter radius:circleRadius];
    [mapView addOverlay:circleOverlay];
    
    //the viewForOverlay delegate method...
    -(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id)overlay
    {
        if ([overlay isKindOfClass:[MKCircle class]])
        {
            MKCircleView* circleView = [[[MKCircleView alloc] initWithOverlay:overlay] autorelease];
            circleView.fillColor = [UIColor blueColor];
            circleView.strokeColor = [UIColor blueColor];
            circleView.lineWidth = 5.0;
            circleView.alpha = 0.20;
            return circleView;
        }
        else
            if ([overlay isKindOfClass:[MKPolygon class]])
            {
                MKPolygonView *polygonView = [[[MKPolygonView alloc] initWithOverlay:overlay] autorelease];
                polygonView.fillColor = [UIColor blueColor];
                polygonView.strokeColor = [UIColor blueColor];
                polygonView.lineWidth = 5.0;
                polygonView.alpha = 0.20;
                return polygonView;
            }
    
        return nil;
    }
    

    You also mention in your edit that “I see the overlays, circles and polygons”. That sounds like you are creating more than one circle and/or polygon overlay. In that case, having just one polygonOverlay and circleOverlay variable won’t work.

    If you really do have multiple overlays of each type, then don’t store references to the overlays. Instead, in the filterAnnotations method, for each annotation, loop through the mapView.overlays array and execute the viewForOverlay and point-in-polygon test in the nested loop.

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

Sidebar

Related Questions

I am working with Mapkit and I have to show annotations in the map
I have this code to show a map using the Virtual Earth API: <script
To draw a circle on map I have a center GLatLng (A) and a
I have to map a REST Webservice URL like http://server:8080/application/service/customer/v1 to createCustomer method in
I have a map with about 80 annotations. I would like to do 3
I currently have a map mashup that has locations that I'm populating from my
I have a show page that should show a business and a google map
I have a detail view that is pushed from a map. I am trying
I have to show a bunch of markers on the map after the map
i have some items on my map. Some of them, friends, have to show

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.