2012-06-06 82 views
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 
    } 
} 
+0

见http://stackoverflow.com/questions/17892362/how-to-delete-the-last-annotation-on-a-mapview – Anna

回答

1

执行以下操作,

如果有注释对象

[self.mapView removeAnnotation:annot]; 

如果你有对象

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

如果我使用[self.mapView removeAnnotation:self.mapView。 annotations.lastObject]; 它删除用户位置 –

+0

[self.mapView removeAnnotation:annot];这项工作,如果我用同样的方法做到这一点,但我怎么能把它放到按钮操作中? –

+0

为一个按钮创建一个ibaction,然后在其中设置代码,您是否遇到问题? –

0

指数这样做是为了删除您最后添加删除注释行动:

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

希望有帮助

+0

这样它删除用户的位置。即使我添加了另一个引脚,它也不会删除它。 –

+0

检查用户的位置,如果计数是1,那么你不想删除它,否则另一个注释将被删除,如果(![self.mapView.annotations count] == 1){[self.mapView removeAnnotation:[self.mapView.annotations lastObject]];} –

+0

现在它不会删除任何内容 –

4

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

[vwMap removeAnnotations:vwMap.annotations]; 

PS:vwMap是MKMap视图对象

+0

upvote for simple and perfect solution – Bond

+0

Downvote for not answers the question.He only only want to remove one annotation。This will remove them all。 –

+0

Downvote for not answering这个问题。 –

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

    } 
} 

并非无懈可击代码,但它可以引导你:-)

0

使用此代码!

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