MapView注释不拖动

发布于 2024-10-18 11:23:09 字数 2042 浏览 4 评论 0原文

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

  -(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 永远不会被调用(我已经在例程中设置了一个断点以确保)。其他一切都工作正常。我的图标以动画方式进入视图等。当我点击图标并按住手指时,图标保持在原位(地图也不会移动,这让我认为我实际上已经点击了该图标)。我是否缺少某种初始化?

I am trying to implement a draggable "pin" (actually a custom icon) in a map view. This is the delegate code that I have:

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

The problem is that didChangeDragState is never being called (I've set a breakpoint in the routine to be sure). Everything else is working fine. My icons animate into the view, etc. When I tap the icon and hold my finger down the icon stays in place (the map doesn't move either which makes me think that I've actually hit the icon). Am I missing some kind of initialization?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

沫尐诺 2024-10-25 11:23:09

知道了!问题出在我的自定义注释类的接口文件中。有问题的行读取:

@property (nonatomic,readonly) CLLocationCoordinate2D   coordinate;

它必须读取:

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

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

Got it! The problem was in the interface file of my custom annotation class. The offending line read:

@property (nonatomic,readonly) CLLocationCoordinate2D   coordinate;

It has to read:

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

I guess that it has to have read/write capability.

逐鹿 2024-10-25 11:23:09

使用 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;

Use MKPinAnnotationView instead of 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;
北方。的韩爷 2024-10-25 11:23:09

这是一个棘手的问题!我刚刚实现了类似的东西(尽管我对 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;
}

This is a tricky one! I've just implemented something similar (though I subclassed MKAnnotationView) and when I tried adding the annotationView:didChange delegate method to my view controller it didn't get called even though I was able to drag the annotation view??

I also copy/pasted your code into my view controller and it worked straight out of the box, with the delegate method being called and all!

The only thing I can think of is that instead of passing mvMap to dequeueReusableAnnotationViewWithIdentifier: try passing the mapView object that is supplied by the delegate method. Based on the code you provided above I am unable to tell if they are the same object so it might be worth a shot?

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

[EDIT TO ADD MY CODE AS REFERENCE]

- (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;
}
携余温的黄昏 2024-10-25 11:23:09

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

if you are using a subview of MKPinAnnotationView and are overriding setSelected(), make sure you call super.setSelected()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文