【问题标题】:MapView Annotation not draggingMapView注释不拖动
【发布时间】:2011-02-21 03:29:09
【问题描述】:

我正在尝试在地图视图中实现可拖动的“图钉”(实际上是自定义图标)。这是我拥有的委托代码:

  -(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
    MKAnnotationView *aView;

    aView=(MKAnnotationView *) [mvMap dequeueReusableAnnotationViewWithIdentifier:annotation.title];
    if (aView==nil) 
        aView=[[[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotation.title] autorelease];
    else
        aView.annotation=annotation;
    [aView setImage:[UIImage imageNamed:selIcon]];
    aView.canShowCallout=TRUE;
    [aView setDraggable:YES];
    return aView;
}
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views { 
    MKAnnotationView *aV; 
    for (aV in views) {
        CGRect endFrame = aV.frame;

        int xDelta=0;
        xDelta=sIcons.selectedSegmentIndex*61+22;
        aV.frame = CGRectMake(aV.frame.origin.x-145+xDelta, aV.frame.origin.y - 150.0, aV.frame.size.width, aV.frame.size.height);

        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.7];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [aV setFrame:endFrame];
        [UIView commitAnimations];
    }
}
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState {
    if (oldState == MKAnnotationViewDragStateDragging) {
        addAnnotation *annotation = (addAnnotation *)view.annotation;
        annotation.subtitle = [NSString stringWithFormat:@"%f %f", annotation.coordinate.latitude, annotation.coordinate.longitude];
    }
    if (newState == MKAnnotationViewDragStateEnding) {
        CLLocationCoordinate2D droppedAt = view.annotation.coordinate;
        NSLog(@"dropped at %f,%f", droppedAt.latitude, droppedAt.longitude);
    }
}

问题是 didChangeDragState 永远不会被调用(我已经在例程中设置了一个断点以确保)。其他一切工作正常。我的图标动画到视图中,等等。当我点击图标并按住我的手指时,图标保持在原位(地图也不会移动,这让我认为我实际上已经点击了图标)。我错过了某种初始化吗?

【问题讨论】:

  • 检查 setCoordinate: 是否在您的注释上实现。这以前给我带来了麻烦。
  • 是的。我想到了。在下面回答。

标签: iphone annotations mapkit


【解决方案1】:

知道了!问题出在我的自定义注释类的接口文件中。违规行如下:

@property (nonatomic,readonly) CLLocationCoordinate2D   coordinate;

它必须阅读:

@property (nonatomic,readwrite,assign) CLLocationCoordinate2D   coordinate;

我猜它必须具有读/写能力。

【讨论】:

  • ...或者让它只读(就像 MKAnnotation 协议参考建议的那样)并实现 setCoordinate。无论哪种方式,都很烦人。感谢您的帮助。
【解决方案2】:

使用MKPinAnnotationView 而不是MKAnnotationView

MKPinAnnotationView *pin = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"purple_pin"];
if (pin==nil)
{
    pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"purple_pin"];} else {
    pin.annotation=annotation;
} 
pin.pinColor = MKPinAnnotationColorPurple;
pin.draggable = TRUE;
return pin;

【讨论】:

    【解决方案3】:

    这是一个棘手的问题!我刚刚实现了类似的东西(尽管我继承了 MKAnnotationView),当我尝试将 annotationView:didChange 委托方法添加到我的视图控制器时,即使我能够拖动注释视图,它也没有被调用??

    我还将您的代码复制/粘贴到我的视图控制器中,它直接开箱即用,调用了委托方法等等!

    我唯一能想到的是,不要将 mvMap 传递给dequeueReusableAnnotationViewWithIdentifier:,而是尝试传递委托方法提供的 mapView 对象。根据您在上面提供的代码,我无法判断它们是否是同一个对象,所以值得一试吗?

    aView=(MKAnnotationView *) [mvMap dequeueReusableAnnotationViewWithIdentifier:annotation.title];
    

    [编辑以添加我的代码作为参考]

    - (MKAnnotationView *)mapView:(MKMapView *)theMapView viewForAnnotation:(id<MKAnnotation>)annotation
    {
        static NSString* ParkAnnotationIdentifier = @"ParkAnnotationIdentifier";
        MKAnnotationView* parkAnnotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:ParkAnnotationIdentifier];
        if (!parkAnnotationView)
        {
            MKAnnotationView *annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation
                                                                             reuseIdentifier:ParkAnnotationIdentifier] autorelease];
            UIImage *imageIcon = [UIImage imageNamed:@"scooterIcon.png"];
            annotationView.image = imageIcon;
            annotationView.draggable = YES;
            return annotationView;
        }
        else
        {
            parkAnnotationView.annotation = annotation;
        }
        return parkAnnotationView;
    }
    

    【讨论】:

    • 我只是没有高兴地尝试过。我相信它们是同一个对象。我还在模拟器中尝试过,看看是否有某种硬件发生,但那里的行为相同。
    • 奇怪!好吧,如果它有帮助,我刚刚发布了我的 annotationForView: 代码和我的答案,以防你看到任何其他可能导致此问题的原因。
    • 感谢您的代码。看起来我们在做基本相同的事情。问题一定出在其他地方,因为没有调用委托方法。浏览完文档后,唯一需要设置的是 draggable=YES (我将代码更改为使用属性而不是代码中的方法)。还有其他人遇到过类似的问题吗?
    【解决方案4】:

    如果您使用 MKPinAnnotationView 的子视图并覆盖 setSelected(),请确保调用 super.setSelected()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-02
      • 2015-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-09
      相关资源
      最近更新 更多