添加另一个时如何删除以前的注释引脚?

S.P*_*lip 3 iphone annotations objective-c mapkit mkmapview

我有一个UITableView,每个单元格都包含一个Map按钮.当我点击每个按钮时,会显示地图视图并添加相应的注释引脚.

点击每个地图按钮时会调用此代码,

    double tempLatitude  = [[[myDict objectForKey:key] objectAtIndex:15] doubleValue];
    double tempLongitude = [[[myDict objectForKey:key] objectAtIndex:16] doubleValue];
                                                  // key is the cell index.

         //--** Setting the Latitude and Longitude 
         CLLocationCoordinate2D myCoordinate;

         myCoordinate.latitude  = tempLatitude;
         myCoordinate.longitude = tempLongitude;

         MyMapAnnotation *MyAnnotation = [[[MyMapAnnotation alloc] initWithCoordinate:myCoordinate addressDictionary:nil]autorelease];
         MyAnnotation.title            = [[myDict objectForKey:key] objectAtIndex:1];
         MyAnnotation.subtitle         = [NSString  stringWithFormat:@"%f %f", MyAnnotation.coordinate.latitude, MyAnnotation.coordinate.longitude];
Run Code Online (Sandbox Code Playgroud)

这是MyMapAnnotation的代码,

    @synthesize coordinate = theCoordinate;
    @synthesize title = theTitleAnnotation;
    @synthesize subtitle = theSubTitleAnnotation;



    - (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate addressDictionary:(NSDictionary *)addressDictionary 
    {

      if ((self = [super initWithCoordinate:coordinate addressDictionary:addressDictionary]))
      {
         self.coordinate = coordinate;
      }
         return self;
    }
Run Code Online (Sandbox Code Playgroud)

一切正常.现在,当我点击一个按钮时,会显示相应的注释引脚,当我点击第二个按钮时,前一个引脚仍然存在,并且还添加了第二个注释引脚.

我想要的是,

当我点击每个按钮时,应删除先前的注释引脚,并且只应添加当前注释引脚.

如何识别上一个引脚?

我在哪里可以提供这段代码[mapView removeAnnotation:annotationToRemove]

编辑:显然,'之前的注释引脚'是'annotationToRemove'.我知道这个.我的问题是,如何识别此前一个注释引脚以指定为'annotationToRemove'?

谢谢 :-)

Mat*_*Mat 6

你试过[myMap removeAnntoation:[myMap.annotations objectAtIndex:0]];吗?

编辑:有几种方法可以做到这一点,一种是设置一个tag注释,然后按标签搜索; 另一种方法是检查所有注释,然后删除你想要的那个,如果你的问题是删除之前添加的注释,你可以调用:

[myMap removeAnntoation:[myMap.annotations lastObject]];

PS:如果你myMap.showsUserLocation=YES;看看这个:如何在不删除蓝点的情况下从MKMapView中删除所有注释?.