2012-06-06 77 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]; 
    } 
}