地图工具包中的拖放注释无法按预期工作

时间:2015-08-05 07:48:30

标签: ios iphone mapkit core-location mapkitannotation

我正在尝试在地图上实现拖放注释。最初引脚被丢弃在用户的当前位置,我使用[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

我缺少什么?

提前致谢。

1 个答案:

答案 0 :(得分:0)

嗨请在这里查看Jordan的答案,为更换引脚提供最佳解决方案Click Here