iphone - 在 map View 上拖动注释图钉

标签 iphone ios draggable mkannotation

注释图钉是可拖动的,但我无法将其拖放到目标位置。问题是我怎样才能得到我放置注释图钉的目标位置的坐标?有什么方法吗?

编辑 1:

注释图钉的中心似乎从未改变过我放置图钉的位置。

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)annotationView didChangeDragState:(MKAnnotationViewDragState)newState 
fromOldState:(MKAnnotationViewDragState)oldState 
{
if (newState == MKAnnotationViewDragStateEnding)
{
    NSLog(@"x is %f", annotationView.center.x);
    NSLog(@"y is %f", annotationView.center.y);

    CGPoint dropPoint = CGPointMake(annotationView.center.x, annotationView.center.y);
    CLLocationCoordinate2D newCoordinate = [self.mapView convertPoint:dropPoint toCoordinateFromView:annotationView.superview];
    [annotationView.annotation setCoordinate:newCoordinate];

}
}

编辑 2:

注释.h

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

注释.m

- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate {
coordinate = newCoordinate; 
}

编辑 3: 无论我在哪里拖动图钉,droppedAt 的 NSLog 输出总是相同的。

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)annotationView didChangeDragState:(MKAnnotationViewDragState)newState 
fromOldState:(MKAnnotationViewDragState)oldState 
{
if (newState == MKAnnotationViewDragStateEnding)
{
    CLLocationCoordinate2D droppedAt = annotationView.annotation.coordinate;
    NSLog(@"Pin dropped at %f,%f", droppedAt.latitude, droppedAt.longitude);
}
}

编辑 4:

注释.m @动态启动地址;

- (CLLocationCoordinate2D)coordinate
{
coordinate.latitude = [self.startAddr.latitude doubleValue];
coordinate.longitude = [self.startAddr.longitude doubleValue];    
return coordinate;
}

- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate {
//I need to update the self.startAddr.latitude and longitude here. Problem solved.
self.startAddr.latitude = [NSNumber numberWithDouble:newCoordinate.latitude];
self.startAddr.longitude = [NSNumber numberWithDouble:newCoordinate.longitude];

coordinate = newCoordinate; 
}

最佳答案

首先你必须像这样创建一个自定义注释:

MyAnnotation.h

#import <MapKit/MapKit.h>

@interface MyAnnotation : NSObject <MKAnnotation>{

    CLLocationCoordinate2D coordinate;

}
- (id)initWithCoordinate:(CLLocationCoordinate2D)coord;
- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate;
@end

MyAnnotation.m

#import "MyAnnotation.h"

@implementation MyAnnotation
@synthesize coordinate;

- (NSString *)subtitle{
    return nil;
}

- (NSString *)title{
    return nil;
}

-(id)initWithCoordinate:(CLLocationCoordinate2D)coord {
    coordinate=coord;
    return self;
}

-(CLLocationCoordinate2D)coord
{
    return coordinate;
}

- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate {
    coordinate = newCoordinate; 
}

@end

之后,你只需要像这样使用你的注解:

// Assuming you have imported MyAnnotation.h and you have a self.map property pointing to a MKMapView
MyAnnotation *myPin = [[MyAnnotation alloc] initWithCoordinate:self.map.centerCoordinate]; // Or whatever coordinates...
[self.map addAnnotation:myPin];

此外,您必须为注释返回一个 View 。您可以这样做:

- (MKAnnotationView *) mapView: (MKMapView *) mapView viewForAnnotation: (id<MKAnnotation>) annotation {
    MKPinAnnotationView *pin = (MKPinAnnotationView *) [self.map dequeueReusableAnnotationViewWithIdentifier: @"myPin"];
    if (pin == nil) {
        pin = [[[MKPinAnnotationView alloc] initWithAnnotation: annotation reuseIdentifier: @"myPin"] autorelease]; // If you use ARC, take out 'autorelease'
    } else {
        pin.annotation = annotation;
    }
    pin.animatesDrop = YES;
    pin.draggable = YES;

    return pin;
}

然后,一旦您在 Controller 中采用了 MKMapViewDelegate 协议(protocol),您就可以像这样拖动后获取图钉的坐标:

- (void)mapView:(MKMapView *)mapView 
 annotationView:(MKAnnotationView *)annotationView 
didChangeDragState:(MKAnnotationViewDragState)newState 
   fromOldState:(MKAnnotationViewDragState)oldState 
{
    if (newState == MKAnnotationViewDragStateEnding)
    {
        CLLocationCoordinate2D droppedAt = annotationView.annotation.coordinate;
        NSLog(@"Pin dropped at %f,%f", droppedAt.latitude, droppedAt.longitude);
    }
}

关于iphone - 在 map View 上拖动注释图钉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10733564/

相关文章:

jQuery 和 jQuery UI : Building a draggable infinite grid with content (imgs) of varying size

javascript - 拖放的最佳轻量级解决方案是什么?

iphone - 整个文件不可用时 MPMoviePlayerViewController 的通知?

iphone - URL 连接 : What is the difference between the following?

ios - 如何缩放 iOs 5+ 的 UI

ios - 在 UITableViewCell 中动态显示图像

c# - 移出表单时更改 DragEffect

iphone - UITableViewCell 设置选中图片

ios - Titanium:无需设备即可生成 IPA

ios - 如何判断用户之前是否订阅过 - RevenueCat