annotationView didChangeDragState被多次触发

时间:2011-06-14 15:16:02

标签: objective-c ios mapkit mkannotation

我在IOS4 mapkit中有一个可拖动的注释,我正在尝试在将注释拖动到新位置时调用一个事件。

我的代码目前看起来像:

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

        //update the annotation
        //see if its an information annotation
        if ([annotationView.annotation isKindOfClass:[InfoAnnotation class]]) {
            NSLog(@"Info annotation updating..");
            InfoAnnotation* userAnnotation = ((InfoAnnotation *)annotationView.annotation);
            [userAnnotation updateLocationWithServerForConvoy: self.title];
        }

    }
}

代码只记录更新,然后告诉注释将其更新发送到我的服务器,这是一种自定义方法。

此方法似乎多次被触发,请参阅此处的日志:

2011-06-15 01:12:39.347 Convoy[1699:207] dropped at 37.340206,-122.027550
2011-06-15 01:12:39.347 Convoy[1699:207] Info annotation updating..
2011-06-15 01:12:39.658 Convoy[1699:207] dropped at 37.340206,-122.027550
2011-06-15 01:12:39.659 Convoy[1699:207] Info annotation updating..
2011-06-15 01:12:39.957 Convoy[1699:207] dropped at 37.340206,-122.027550
2011-06-15 01:12:39.958 Convoy[1699:207] Info annotation updating..


2011-06-15 01:12:43.415 Convoy[1699:207] dropped at 37.339251,-122.026691
2011-06-15 01:12:43.416 Convoy[1699:207] Info annotation updating..
2011-06-15 01:12:43.713 Convoy[1699:207] dropped at 37.339251,-122.026691
2011-06-15 01:12:43.713 Convoy[1699:207] Info annotation updating..
2011-06-15 01:12:44.006 Convoy[1699:207] dropped at 37.339251,-122.026691
2011-06-15 01:12:44.006 Convoy[1699:207] Info annotation updating..
2011-06-15 01:12:44.297 Convoy[1699:207] dropped at 37.339251,-122.026691
2011-06-15 01:12:44.297 Convoy[1699:207] Info annotation updating..


2011-06-15 01:12:54.825 Convoy[1699:207] dropped at 37.337135,-122.025833
2011-06-15 01:12:54.825 Convoy[1699:207] Info annotation updating..
2011-06-15 01:12:55.150 Convoy[1699:207] dropped at 37.337135,-122.025833
2011-06-15 01:12:55.150 Convoy[1699:207] Info annotation updating..
2011-06-15 01:12:55.475 Convoy[1699:207] dropped at 37.337135,-122.025833
2011-06-15 01:12:55.476 Convoy[1699:207] Info annotation updating..
2011-06-15 01:12:55.771 Convoy[1699:207] dropped at 37.337135,-122.025833
2011-06-15 01:12:55.772 Convoy[1699:207] Info annotation updating..
2011-06-15 01:12:56.070 Convoy[1699:207] dropped at 37.337135,-122.025833
2011-06-15 01:12:56.070 Convoy[1699:207] Info annotation updating..

每次我拖动它(即在间隙中)它似乎会增加1次它被调用的次数。任何人都可以告诉我可能导致这种情况的原因吗?

2 个答案:

答案 0 :(得分:4)

如果有人还在想 - 你必须将MKAnnotationView拖动状态设置为 MKAnnotationViewDragStateNone

所以代码是:

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)annotationView didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState
{
  if (newState == MKAnnotationViewDragStateEnding)
  {
    /* ... */
    [annotationView setDragState:MKAnnotationViewDragStateNone];
    // If you are animating - move the above into the completion block
  }
}

答案 1 :(得分:0)

这看起来很奇怪。你有没有考虑过简单地挂钩你的注释的setCoordinate方法?只要通过拖动移动MKMapAnnotation,MapKit就会调用此方法。