0

我在 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
    }
}
4

5 回答 5

4

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

[vwMap removeAnnotations:vwMap.annotations];

PS:vwMap是MKMap视图对象

于 2014-06-05T15:39:45.397 回答
2

请执行下列操作,

如果您有注释对象

[self.mapView removeAnnotation:annot];

如果你有对象的索引

[self.mapView removeAnnotation:self.mapView.annotations.lastObject];
于 2012-06-06T08:30:36.030 回答
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;
    }

   }
}

不是完美的代码,但它可以指导你:-)

于 2015-03-07T01:12:21.980 回答
0

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

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

希望有帮助

于 2012-06-06T08:41:38.007 回答
0

使用此代码!

NSArray *array=self.mapview.annotations;
for (MKPointAnnotation *anno in array)
{
    if(anno==[array lastObject])
    {
        [self.mapview removeAnnotation:anno];
    }
}
于 2016-02-03T10:36:13.673 回答