callback when dragging annotation

拈花ヽ惹草 提交于 2019-12-12 13:33:28

问题


I can drag my annotation and when I drop it I can read the position. However, I need to constantly update my title with the position I'm dragging. I've tried with adding a UIPanGestureRecognizer but that doesn't work when I dragging the annotation. What method shall I use to get constantly calls back to my code while dragging an annotation?

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view
                didChangeDragState:(MKAnnotationViewDragState)newState
                fromOldState:(MKAnnotationViewDragState)oldState {

CLLocationCoordinate2D annoCoord = view.annotation.coordinate;

switch (newState) {
    case MKAnnotationViewDragStateStarting:
        NSLog(@"Start dragging annotation");
        break;

    case MKAnnotationViewDragStateDragging:
        NSLog(@"Dragging annotation");
        break;

    case MKAnnotationViewDragStateEnding:
        [view setDragState:MKAnnotationViewDragStateNone]; // must be here! else multiple calls
        NSLog(@"Ending dragging annotation at %f : %f", annoCoord.latitude, annoCoord.longitude);
        break;

    case MKAnnotationViewDragStateCanceling:
        NSLog(@"Cancel dragging annotation");
        break;

    case MKAnnotationViewDragStateNone:
        NSLog(@"None dragging annotation");
        break;
    default:
        break;
    }

}

I only get callbacks when a state starts, not during the dragging.

Any help would be much appreciated.

cheers, tord


回答1:


You can set an observer on the MKAnnotationView center property. Then in the callback for the observer, you can convert the annotation view center location from screen coordinates to geo coordinates.

[myAnnotationView addObserver:myMapViewDelegate forKeyPath:@"center" options:NSKeyValueObservingOptionNew context:nil];
...
- (void)observeValueForKeyPath:(NSString *)keyPath
                  ofObject:(id)object
                    change:(NSDictionary *)change
                   context:(void *)context
{
    CGPoint position = myAnnotationView.center;
    //... here take  myAnnotationView.centerOffset into consideration to get the correct coordinate  
    CLLocationCoordinate2D newCoordinate = [self.mapView convertPoint:position toCoordinateFromView:self.superview];
}


来源:https://stackoverflow.com/questions/20918210/callback-when-dragging-annotation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!