如何在IOS上管理MKAnnotationView的拖放?

Chr*_*ove 11 iphone drag-and-drop mkmapview ios

我没有工作InterfaceBuilder.

我有一个实例MKAnnotationViewsetDraggableYES,在我的MKMapView我的标注视图显示,我可以将它拖放.

如何执行放置操作时执行方法?在这个方法中,我需要我的注释视图的新协调.

小智 34

如果您正确地使用setCoordinate方法设置MKAnnotation对象,那么在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);
    }
}
Run Code Online (Sandbox Code Playgroud)

有关参考,请参阅此处文档中的 "将注释视图标记为可拖动"部分.如果您的应用需要在早于4.x的操作系统中工作,则拖动需要更多的手动工作.文档中的链接还指出了如果需要如何执行此操作的示例.


kev*_*inl 7

您可能还想添加以下内容:

if (newState == MKAnnotationViewDragStateStarting) {
    annotationView.dragState = MKAnnotationViewDragStateDragging;
}
else if (newState == MKAnnotationViewDragStateEnding || newState == MKAnnotationViewDragStateCanceling) {
    annotationView.dragState = MKAnnotationViewDragStateNone;
}
Run Code Online (Sandbox Code Playgroud)

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