0

我正在尝试在地图上实现拖放注释。最初,引脚被放置在用户的当前位置,[self.mapView setShowsUserLocation:YES];但当我拖放引脚时,它会在一段时间后返回到当前位置。

这是代码:

    #import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize mapView;

- (void)viewDidLoad {
    [super viewDidLoad];
    mapView.delegate = self;
}


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

    //MKAnnotationView* annotationView = nil;

    MKAnnotationView *annotationView = [mv dequeueReusableAnnotationViewWithIdentifier:@"PinAnnotationView"];

    if (!annotationView) {
        annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"PinAnnotationView"];
        annotationView.draggable = YES;

    }
    [self.locationManager stopUpdatingLocation];
    return annotationView;
}

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)annotationView didChangeDragState:(MKAnnotationViewDragState)newState fromOldState:(MKAnnotationViewDragState)oldState
{

    if(newState == MKAnnotationViewDragStateStarting)  {
        CLLocationCoordinate2D droppedFrom = annotationView.annotation.coordinate;
        NSLog(@"dropped from %f, %f", droppedFrom.latitude , droppedFrom.longitude);
       // annotationView.dragState = MKAnnotationViewDragStateDragging;
    }

    else if (newState == MKAnnotationViewDragStateEnding)
    {
        CLLocationCoordinate2D droppedAt = annotationView.annotation.coordinate;
        NSLog(@"dropped at %f,%f", droppedAt.latitude, droppedAt.longitude);
        annotationView.dragState = MKAnnotationViewDragStateNone;
    }
    else if (newState == MKAnnotationViewDragStateCanceling) {
        // custom code when drag canceled...

        // tell the annotation view that the drag is done
        [annotationView setDragState:MKAnnotationViewDragStateNone animated:YES];
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [self.mapView setShowsUserLocation:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

我错过了什么?

提前致谢。

4

1 回答 1

0

嗨,请查看 Jordan 的答案,这是更换引脚的最佳解决方案,请单击此处

于 2015-08-05T11:26:48.790 回答