我可以拖动我的注释,当我放下它时我可以读取位置。但是,我需要不断地用我拖动的位置更新我的标题。我试过添加一个 UIPanGestureRecognizer,但是当我拖动注释时它不起作用。在拖动注释时,我应该使用什么方法来不断回调我的代码?
-(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;
}
}
我只会在状态开始时收到回调,在拖动期间不会。
如有任何帮助,我们将不胜感激。
干杯,加油
最佳答案
您可以在 MKAnnotationView 中心属性上设置观察者。然后在观察者的回调中,您可以将注释 View 中心位置从屏幕坐标转换为地理坐标。
[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];
}
关于ios - 拖动注释时的回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20918210/