当回调拖动注释 [英] callback when dragging annotation

查看:179
本文介绍了当回调拖动注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以将我的注解,当我砸我可以读取的位置。但是,我需要不断地更新我的,我拖的位置称号。我试着添加UIPanGestureRecognizer但是当我拖着注释不起作用。什么方法应我用得到不断呼吁回到我的code,同时拖动一个注释?

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.

任何帮助将是非常美联社preciated。

Any help would be much appreciated.

欢呼声中,托德

推荐答案

您可以设置在MKAnnotationView中心物业的观察员。然后在回调的观察者,你可以转换屏幕坐标地理坐标标注视图的中心位置。

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];
}

这篇关于当回调拖动注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆