启用和禁用注释拖动(即时)(iOS Mapkit)

我创build了一个mapview,它有一个button,可以根据项目需求在“编辑”模式和“拖动”模式之间切换。 我意识到通过在viewForAnnotation中设置它们可拖动的方式可以使拖动的注释变得容易,但是所需的行为不允许这样做。 我尝试了几种不同的方式来将注释更改为可拖动而没有成功。 首先想到的是循环现有的注释,并设置每个“可拖动”和“选定”,但我得到一个无法识别的select发送到实例错误(我尝试实例化一个新的注释传入的对象和重绘而在循环中,但我也得到相同的错误):

NSLog(@"Array Size: %@", [NSString stringWithFormat:@"%i", [mapView.annotations count]]); for(int index = 0; index < [mapView.annotations count]; index++) { if([[mapView.annotations objectAtIndex:index]isKindOfClass:[locAnno class]]){ NSLog(@"** Location Annotation at Index: %@", [NSString stringWithFormat:@"%i", index]); NSLog(@"* Location Marker: %@", [mapView.annotations objectAtIndex:index]); } if([[mapView.annotations objectAtIndex:index]isKindOfClass:[hydAnno class]]) { NSLog(@"** Hydrant Annotation at Index: %@", [NSString stringWithFormat:@"%i", index]); NSLog(@"* Hydrant Marker: %@", [mapView.annotations objectAtIndex:index]); [[mapView.annotations objectAtIndex:index]setSelected:YES]; [[mapView.annotations objectAtIndex:index]setDraggable:YES]; } } 

第二个想法是使用'didSelectAnnotationView',并在选中时在注释中设置select和可拖动,并在模式重新切换时重置属性。 这种方法很有效,但是效果并不好,因为事件并不总是会触发,而且在您更改属性之前,您需要一次或多次点击注释。

 - (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view { NSLog(@"Annotation Selected!"); if(!editMode) { view.selected = YES; view.draggable = YES; } 

}

第一次尝试似乎是最简单的解决scheme,如果我能得到它的工作。 另一方面,使用didSelect方法非常麻烦,也很麻烦。 我对iOS开发很陌生,所以我很抱歉,如果我忽略了一些新手,同时扼杀这个。 我很欣赏社区可以提供的任何见解。 非常感谢。

第一种方法比使用didSelectAnnotationView委托方法更好。

导致“无法识别的select器”错误的代码的问题在于,它在注释对象(typesid<MKAnnotation> )上调用setSelected:setDraggable:而不是相应的MKAnnotationView对象。 id<MKAnnotation>对象没有这样的方法,所以你得到“无法识别的select器”的错误。

地图视图的annotations数组包含对id<MKAnnotation> (数据模型)对象的引用,而不是这些注释的MKAnnotationView对象。

所以你需要改变这个:

 [[mapView.annotations objectAtIndex:index]setSelected:YES]; [[mapView.annotations objectAtIndex:index]setDraggable:YES]; 

像这样的东西:

 //Declare a short-named local var to refer to the current annotation... id<MKAnnotation> ann = [mapView.annotations objectAtIndex:index]; //MKAnnotationView has a "selected" property but the docs say not to set //it directly. Instead, call deselectAnnotation on the annotation... [mapView deselectAnnotation:ann animated:NO]; //To update the draggable property on the annotation view, get the //annotation's current view using the viewForAnnotation method... MKAnnotationView *av = [mapView viewForAnnotation:ann]; av.draggable = editMode; 

您还必须更新viewForAnnotation 委托方法中的代码,以便它也将draggable设置为editMode而不是硬编码的YESNO以便如果地图视图需要在已经更新重新创build注释的视图它在for循环中,注释视图将具有draggable的正确值。