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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10910568/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:30:54  来源:igfitidea点击:

How to remove just added, one annotation?

iosxcodeannotations

提问by Pavel Kaljunen

I have a lot of annotations on the mapView and user location dot. Then, if user tap for 2 sec. on the map, I add an extra annotation with options. I need to remove that last added annotation from map by pressing the button. How can I remove it without to remove any other annotation?

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

回答by pkc456

To remove all the annotations from map view:

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

[vwMap removeAnnotations:vwMap.annotations];

PS: vwMap is the MKMap view object

PS:vwMap是MKMap视图对象

回答by Omar Abdelhafith

Do the following,

请执行下列操作,

If you have the annotation object

如果你有注解对象

[self.mapView removeAnnotation:annot];

If you have the index of the object

如果你有对象的索引

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

回答by Paresh Navadiya

Do this to remove your last added annotation in your delete Action:

这样做可以删除您在删除操作中最后添加的注释:

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

Hope helpful

希望有帮助

回答by r farnell

I managed to remove the annotation object that is touched by doing the following, I know this wasn't the question but it may help someone out

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

set the mapView as delegate

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

   }
}

not flawless code but it may guide you :-)

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

回答by bharathi kumar

Use this code!

使用此代码!

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