iphone - 添加另一个时如何删除以前的注释图钉?

标签 iphone objective-c annotations mkmapview mapkit

我有一个 UITableView,每个单元格都包含一个 map 按钮。当我点击每个按钮时,会显示 map View 并添加相应的注释图钉。

当我点击每个 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];

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

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

但是

我想要的是,

当我点击每个按钮时,应该删除之前的注释图钉,只添加当前的注释图钉

如何识别之前的 PIN 码?

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

编辑:显然,“上一个注释图钉”是“annotationToRemove”。我知道这个。 我的问题是,如何识别之前的注释 pin 以指定为“annotationToRemove”?

谢谢:-)

最佳答案

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

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

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

P.S:如果你有 myMap.showsUserLocation=YES; 看这个:How to remove all annotations from MKMapView without removing the blue dot? .

关于iphone - 添加另一个时如何删除以前的注释图钉?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6694009/

相关文章:

ios - 关于 dealloc 方法的一些事情

c++ - 有没有一种好方法,可以用 C++ 生成代码以通过网络发送函数参数?

ios - 在 uitextview 中触摸返回时键盘不隐藏

iphone - 从具有多对多关系核心数据 iOS 的上下文中删除 nsmanagedobject

ios - 如何使用 UIButton 为单元格设置动画?

ios - 在 Xamarin.iOS DYLD 中集成时,代码签名对于 iOS Objective C Umbrella 框架无效,库未加载 : App Crash

php - Symfony 将参数传递给验证器约束

java - 如何以编程方式读取方法返回类型上定义的注释

iphone - 我在哪里可以免费在线存储我的 plist,我的应用程序可以下载它?

ios - 终止 iOS 应用程序