我有一个 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/