how to manage drag and drop for MKAnnotationView on IOS?

后端 未结 3 662
青春惊慌失措
青春惊慌失措 2020-12-03 17:44

I\'m working without InterfaceBuilder.

I\'ve an instance of MKAnnotationView with setDraggable on YES, In My MK

3条回答
  •  有刺的猬
    2020-12-03 18:23

    If you've setup your MKAnnotation object with a setCoordinate method properly, then in the didChangeDragState method, the new coordinate should already be in the annotation object:

    - (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);
        }
    }
    

    For reference, see the "Marking Your Annotation View as Draggable" section in the docs here. If your app needs to work in an OS earlier than 4.x, the dragging requires more manual work. The link in the docs also points to an example of how to do that if you need to.

提交回复
热议问题