添加另一个注释引脚时如何删除先前的注释引脚?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您尝试过
[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?.如果您在另一个视图中显示地图视图意味着只需使用
If you are showing the Mapview in Another View means just use
您可以使用 [MyAnnotation setHidden:YES];关闭该引脚并稍后再次打开。
You can use [MyAnnotation setHidden:YES]; to turn the pin off and turn it on again later.
试试这个
try this
这个在 Swift 5 上对我有用:
This one worked for me on Swift 5: