如何pipe理在iOS上的MKAnnotationView的拖放?

我正在没有InterfaceBuilder的工作。

我有一个MKAnnotationView与YES的setDraggable实例,在我的MKMapView我的注释视图显示,我可以拖放它。

执行放置操作时如何执行一个方法? 在这个方法中,我需要我的注释视图的新的坐标。

如果你已经正确的设置了你的MKAnnotation对象的setCoordinate方法,那么在didChangeDragState方法中,新的坐标应该已经在注解对象中:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)annotationView didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState { if (newState == MKAnnotationViewDragStateEnding) { CLLocationCoordinate2D droppedAt = annotationView.annotation.coordinate; NSLog(@"dropped at %f,%f", droppedAt.latitude, droppedAt.longitude); } } 

有关参考,请参阅文档中的“将您的注释视图标记为可拖动”部分。 如果您的应用程序需要在4.x以前的操作系统中工作,则拖动需要更多的手动工作。 文档中的链接也指出了如果需要的话,如何做到这一点的一个例子。

您可能还需要添加以下内容:

 if (newState == MKAnnotationViewDragStateStarting) { annotationView.dragState = MKAnnotationViewDragStateDragging; } else if (newState == MKAnnotationViewDragStateEnding || newState == MKAnnotationViewDragStateCanceling) { annotationView.dragState = MKAnnotationViewDragStateNone; } 

因为MKAnnotationView并没有准确地改变拖拽状态(即使停止拖拽也可以使你的地图与你的注解平移)