繁体   English   中英

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

[英]How to remove previous Annotation pin when adding another one?

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

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

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

我想要的是,

当我点击每个按钮时,应该删除以前的注释引脚,只应该添加当前的注释引脚

如何识别以前的引脚?

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

编辑:显然,“以前的注释针”是“annotationToRemove”。 我知道这个。 我的问题是,如何识别这个以前的注释引脚以指定为“annotationToRemove”?

谢谢:-)

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

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

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

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

如果您在另一个视图中显示 Mapview 意味着只需使用

-(void)viewWillappear
{
[myMap removeAnnotations:toRemove];
}

您可以使用 [MyAnnotation setHidden:YES]; 关闭引脚并稍后再次打开。

尝试这个

-(void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view 
{
   [mapView removeAnnotation:annotationToRemove]
}

这个在Swift 5上为我工作:

mapView.removeAnnotations([mapView.annotations].last)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM