如何仅添加一个MKOverlay ONCE?

时间:2012-12-29 15:21:11

标签: objective-c ios map overlay mapkit

我有一个MKOverlay,在CALayer上有一系列动画图像(在MKOverlay上)。每当我在地图上移动或缩放时,都会再次添加MKOverlay,这会导致同一个叠加层的多个版本一次又一次出现。有没有办法可以将叠加设置为仅显示一次?

这是我在地图上添加叠加层的代码:

    - (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;
}

1 个答案:

答案 0 :(得分:1)

您可以尝试删除这样的圈子:

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