MapView Annotation not dragging

て烟熏妆下的殇ゞ 提交于 2019-12-19 08:26:22

问题


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?


回答1:


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.




回答2:


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;



回答3:


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



回答4:


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



来源:https://stackoverflow.com/questions/5061976/mapview-annotation-not-dragging

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!