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

发布于 2024-11-23 16:16:31 字数 1881 浏览 1 评论 0原文

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

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

    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] ??

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

谢谢:-)

I have a UITableView and each cell contains a Map button. When I tap each button, Map view is shown and corresponding Annotation pin is added.

This code is called when I tap each map button,

    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];

And this is the code of 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;
    }

Everything works fine. Now, when I tap one button, corresponding annotation pin is shown and when I tap second button, the previous pin is still there and second annotation pin is also being added.

But

What I want is,

When I tap each button, the previous annotation pin should be removed and only the current annotation pin should be added.

How can I identify the previous pin?

And where can I give this code [mapView removeAnnotation:annotationToRemove] ??

EDIT: Obviously, the 'previous annotation pin' is the 'annotationToRemove'. I know this. My question is, How can I identify this previous annotation pin to specify as 'annotationToRemove'??

Thanks :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

腹黑女流氓 2024-11-30 16:16:31

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

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

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

PS :如果你有 myMap.showsUserLocation=YES; 看看这个:如何从 MKMapView 中删除所有注释,而不需要删除蓝点?

have you try with [myMap removeAnntoation:[myMap.annotations objectAtIndex:0]]; ?

EDIT:
There are several ways to do this, one is to set a tag to the annotation, then search it by tag; another way is to check all annotations and then remove that which you want, if your problem is to remove the previous annotation added, you can call:

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

P.S: if you have myMap.showsUserLocation=YES; look this: How to remove all annotations from MKMapView without removing the blue dot?.

蓝礼 2024-11-30 16:16:31

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

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

If you are showing the Mapview in Another View means just use

-(void)viewWillappear
{
[myMap removeAnnotations:toRemove];
}
青巷忧颜 2024-11-30 16:16:31

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

You can use [MyAnnotation setHidden:YES]; to turn the pin off and turn it on again later.

℡寂寞咖啡 2024-11-30 16:16:31

试试这个

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

try this

-(void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view 
{
   [mapView removeAnnotation:annotationToRemove]
}
趴在窗边数星星i 2024-11-30 16:16:31

这个在 Swift 5 上对我有用:

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

This one worked for me on Swift 5:

mapView.removeAnnotations([mapView.annotations].last)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文