2012-02-13 64 views
1

爲什麼我的代碼不會刪除貼圖註釋?刪除按鈕上的貼圖註釋單擊(創建的註釋)

我點擊按鈕,它獲取用戶的位置,然後將其釘在地圖上。問題是,當按鈕被擊中第二,第三,.......時間它只是添加引腳而不是刪除第一個引腳(模擬替換引腳)。

我已經嘗試添加另一個按鈕(清除引腳),並遵循此線程:http://stackoverflow.com/questions/3027392/how-to-delete-all-annotations-on-a-mkmapview,但該應用程序只是崩潰,沒有調試信息。

但理論上我的代碼應該工作。那麼我不瞭解什麼?

- (IBAction)getLocation:(id)sender { 
    MapAnnotation *ann1 =[[[MapAnnotation alloc] init]autorelease]; 

    // remove annotation 
    [mapView removeAnnotation:ann1]; 


    locationManager = [[CLLocationManager alloc] init]; 
    locationManager.distanceFilter=kCLDistanceFilterNone; 
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; 
    [locationManager startUpdatingLocation]; 

    [mapView setMapType:MKMapTypeStandard]; 
    [mapView setZoomEnabled:YES]; 
    [mapView setScrollEnabled:YES]; 
    MKCoordinateRegion region = {{0.0,0.0},{0.0,0.0}}; 
    region.center.latitude = locationManager.location.coordinate.latitude; 
    region.center.longitude = locationManager.location.coordinate.longitude; 
    region.span.longitudeDelta = 0.005f; 
    region.span.latitudeDelta = 0.005f; 
    [mapView setRegion:region animated:YES]; 
    [mapView setDelegate:sender]; 

    MKCoordinateRegion location1; 
    location1.center.latitude =locationManager.location.coordinate.latitude; 
    location1.center.longitude= locationManager.location.coordinate.longitude; 
    location1.span.longitudeDelta=0.1; 
    location1.span.latitudeDelta =0.1; 


    // Add Annotation 
    //MapAnnotation *ann1 =[[MapAnnotation alloc] init]; 
    [email protected]"You Parked Here"; 
    [email protected]""; 
    ann1.coordinate= location1.center; 
    [mapView addAnnotation:ann1]; 

} 

回答

3

你刪除您剛剛創建的註釋,並沒有被添加到地圖:

MapAnnotation *ann1 =[[[MapAnnotation alloc] init]autorelease]; 
// remove annotation 
[mapView removeAnnotation:ann1]; 

您可以在地圖上找到註解,然後將其刪除,或者你可以採取嚴厲的做法,只需一次性刪除所有註釋。由於我不知道UI交互,所以我無法真正地概述如何爲您找到註釋。然而,這裏是如何刪除所有:

NSMutableArray *annotations = [NSMutableArray array]; 
for (id annotation in [mapView annotations]) 
{ 
    [annotations addObject:annotation]; 
} 
[mapView removeAnnotations:annotations]; 

HTH。

+0

非常感謝。我試圖「的NSArray *註釋= [MapView的註解]; 爲(在註釋ID註釋) { 如果([註釋isKindOfClass:[MKUserLocation類]]) { 繼續; [MapView的removeAnnotation:註釋] } ; } 「但它不斷崩潰。這很好用 – Rick 2012-02-13 18:23:14

+0

聲音可能...你不想在使用它時改變陣列。 – joshpaul 2012-02-14 01:52:02

+0

這個邏輯,我們可以用來從地圖刪除引腳正確,如果我想從數據庫中刪除引腳記錄?當我點擊銷釘,我得到的所有記錄與pin_id,但我無法找到哪個pin_id單擊針?請你幫忙從這種情況出來。 – 2013-02-25 13:44:49