如何删除刚刚添加的一个注释?

时间:2012-06-06 08:18:44

标签: ios xcode annotations

我在mapView和用户位置点上有很多注释。然后,如果用户点击2秒。在地图上,我添加了一个带有选项的额外注释。我需要通过按下按钮从地图中删除最后添加的注释。如何在不删除任何其他注释的情况下将其删除?

- (void)addPin:(UILongPressGestureRecognizer*)recognizer { 
    if(UIGestureRecognizerStateBegan == recognizer.state) {


        CGPoint tappedPoint = [recognizer locationInView:mapView];

        CLLocationCoordinate2D locCoord= [mapView convertPoint:tappedPoint toCoordinateFromView:mapView];


        MKPointAnnotation *annot = [[MKPointAnnotation alloc] init];
        annot.coordinate = locCoord;
        [self.mapView addAnnotation:annot];

    }

    if(UIGestureRecognizerStateChanged == recognizer.state) {
        // Do repeated work here (repeats continuously) while finger is down
    }

    if(UIGestureRecognizerStateEnded == recognizer.state) {
        // Do end work here when finger is lifted
    }
}

5 个答案:

答案 0 :(得分:4)

从地图视图中删除所有注释:

[vwMap removeAnnotations:vwMap.annotations];

PS:vwMap是MKMap视图对象

答案 1 :(得分:2)

执行以下操作,

如果您有注释对象

[self.mapView removeAnnotation:annot];

如果你有对象的索引

[self.mapView removeAnnotation:self.mapView.annotations.lastObject];

答案 2 :(得分:0)

执行此操作以删除删除操作中最后添加的注释:

[self.mapView removeAnnotation:[self.mapView.annotations lastObject]];

希望有用

答案 3 :(得分:0)

我设法删除了通过执行以下操作而触及的注释对象,我知道这不是问题,但它可以帮助某人

将mapView设置为委托

- (void)mapView:(MKMapView *)thisMapView didSelectAnnotationView:(MKAnnotationView *)view {

MKPointAnnotation *thisTouchedAnnotation = view.annotation;

uint8_t annotationCount = thisMapView.annotations.count;

for(int i =0; i<annotationCount; i++)
{
    if ([thisMapView.annotations objectAtIndex:i]==thisTouchedAnnotation){
        [thisMapView removeAnnotation:[mapView.annotations objectAtIndex:i]];
        break;
    }

   }
}

不是完美无瑕的代码,但它可能会指导您: - )

答案 4 :(得分:0)

使用此代码!

NSArray *array=self.mapview.annotations;
for (MKPointAnnotation *anno in array)
{
    if(anno==[array lastObject])
    {
        [self.mapview removeAnnotation:anno];
    }
}