sridvijay
sridvijay

Reputation: 1526

How do I add an MKOverlay only ONCE?

I have an MKOverlay with a series of animated images on a CALayer (on the MKOverlay). Whenever I move or zoom on the map, the MKOverlay is added again, this causes multiple versions of the same overlay to appear again and again. Is there a way where I can set the overlay to only show once?

Here's my code on adding the Overlay to the map:

    - (void)mapRadar {        
    [self.mapView removeOverlay:self.mapOverlay];


    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    self.mapOverlay = [[MapOverlay alloc] initWithLowerLeftCoordinate:CLLocationCoordinate2DMake(appDelegate.south, appDelegate.west) withUpperRightCoordinate:CLLocationCoordinate2DMake(appDelegate.north, appDelegate.east)];

    self.mapView.showsUserLocation = YES;
    MKMapPoint lowerLeft2 = MKMapPointForCoordinate(CLLocationCoordinate2DMake(appDelegate.south2, appDelegate.west2) );
    MKMapPoint upperRight2 = MKMapPointForCoordinate(CLLocationCoordinate2DMake(appDelegate.north2, appDelegate.east2));

    MKMapRect localMapRect = MKMapRectMake(lowerLeft2.x, upperRight2.y, upperRight2.x - lowerLeft2.x, lowerLeft2.y - upperRight2.y);

      MKMapPoint lowerLeft = MKMapPointForCoordinate(CLLocationCoordinate2DMake(appDelegate.south, appDelegate.west) );
      MKMapPoint upperRight = MKMapPointForCoordinate(CLLocationCoordinate2DMake(appDelegate.north, appDelegate.east));
      MKMapRect nationalSectorMapRect = MKMapRectMake(lowerLeft.x, upperRight.y, upperRight.x - lowerLeft.x, lowerLeft.y - upperRight.y);

    [self.mapView addOverlay:[MKCircle circleWithMapRect:nationalSectorMapRect]];
    [self.mapView setNeedsDisplay];

    [self.mapView setVisibleMapRect:localMapRect animated:YES];
}

#pragma Mark - MKOverlayDelgateMethods

-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id)overlay{

    MapOverlayView* circleView = [[MapOverlayView alloc] initWithCircle:(MKCircle *)overlay];

    return circleView;
}

Upvotes: 1

Views: 408

Answers (1)

Shmidt
Shmidt

Reputation: 16664

You can try to remove circle like this:

for (id<MKOverlay> overlayToRemove in _mapView.overlays)
{
    if ([overlayToRemove isKindOfClass:[MKCircle class]])
    {
        [_mapView removeOverlay:overlayToRemove];
    }
}

Upvotes: 1

Related Questions