2011-06-27 45 views
1

我有一個項目列表表。當我選擇我的一排桌子時,我會顯示包含更多信息的視圖以及帶有地標的地圖。 所使用的代碼如下:從mkmapview刪除以前的地標

MKCoordinateRegion region; 
CLLocationCoordinate2D coordinate = {lat,longt}; 
region.center.latitude = coordinate.latitude; 
region.center.longitude = coordinate.longitude; 
region.span.latitudeDelta = 0.3; 
region.span.longitudeDelta = 0.3; 
[self.mapView setRegion:region animated:YES]; 

MKPlacemark *mPlacemark = [[[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil] autorelease]; 
[mapView addAnnotation:mPlacemark]; 
[mPlacemark release]; 

的問題是,當我選擇另一項目後,我的地圖以前選定的項目顯示新的地標和以前。如何從我的地圖中刪除以前的地標?

回答

2

嘗試刪除以前添加的地標。

- (void)removeAnnotation:(id <MKAnnotation>)annotation 

像: 聲明mPreviousPlacemark爲你的類的retain財產。

[mapView removeAnnotation:mPreviousPlacemark]; 
[mapView addAnnotation:mPlacemark]; 
self.mPreviousPlacemark = mPlacemark; 
[mPlacemark release]; 
+0

與此同時,當我第二次選擇一個新項目,應用程序崩潰,但控制檯不顯示任何錯誤。 – davideagostini

+0

@ reb2awrl:您能展示嗎?,您如何在**。h **中聲明'mPreviousPlacemark'?你有沒有在'mPreviousPlacemark'中使用'@ synthesize',就像** @在你的**。m **類中合成mPreviousPlacemark **。 – Jhaliya

+0

是的我在.h和.m文件中聲明瞭mPreviousPlacemark我使用了@synthesize mPreviousPlacemark。在viewWillAppear之後,我用你的代碼修改了我以前的代碼。 – davideagostini