13

注釈ピンはドラッグ可能ですが、目的の場所にドロップできませんでした。問題は、注釈ピンをドロップした場所の座標をどのように取得できるかです。方法はありますか?

編集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:

Annotaion.m @dynamic startAddr;

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

6 に答える 6

39

まず、次のようなカスタム アノテーションを作成する必要があります。

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

また、注釈のビューを返す必要があります。次のように行うことができます。

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

次に、MKMapViewDelegateコントローラーでプロトコルを採用したら、次のようにピンをドラッグした後、ピンの座標を取得します。

- (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);
    }
}
于 2012-05-24T09:25:19.383 に答える
1
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
{
    MKAnnotationView *a = [ [ MKAnnotationView alloc ] initWithAnnotation:annotation reuseIdentifier:@"currentloc"];
    //  a.title = @"test";
    if ( a == nil )
        a = [ [ MKAnnotationView alloc ] initWithAnnotation:annotation reuseIdentifier: @"currentloc" ];

    NSLog(@"%f",a.annotation.coordinate.latitude);
    NSLog(@"%f",a.annotation.coordinate.longitude);

    CLLocation* currentLocationMap = [[[CLLocation alloc] initWithLatitude:a.annotation.coordinate.latitude longitude:a.annotation.coordinate.longitude] autorelease];
    [self coordinatesToDMS:currentLocationMap];


    MKPinAnnotationView *annView=[[MKPinAnnotationView alloc] initWithAnnotation:a reuseIdentifier:@"currentloc"];
    if(a.annotation.coordinate.longitude == mapView.userLocation.coordinate.longitude ||  a.annotation.coordinate.latitude == mapView.userLocation.coordinate.latitude  )
    {
        if ([annotation isKindOfClass:MKUserLocation.class])
        {
            //user location view is being requested,
            //return nil so it uses the default which is a blue dot...
            return nil;
        }       
        //a.image = [UIImage imageNamed:@"userlocation.png"];
        //a.pinColor=[UIColor redColor];
    }
    else 
    {

        // annView.image =[UIImage imageNamed:@"map-pin.png"];
        //PinFirst=FALSE;
        //annView.pinColor = [UIColor redColor];
        // annView.annotation=annotation;
    }

    annView.animatesDrop = YES;
    annView.draggable = YES;

    annView.canShowCallout = YES;

    return annView;
}

- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
    NSLog(@"map Drag");
}

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view didChangeDragState:(MKAnnotationViewDragState)newState
fromOldState:(MKAnnotationViewDragState)oldState;
{
    NSLog(@"pin Drag");

    if (newState == MKAnnotationViewDragStateEnding)
    {
        CLLocationCoordinate2D droppedAt = view.annotation.coordinate;
        NSLog(@"Pin dropped at %f,%f", droppedAt.latitude, droppedAt.longitude);

        CLLocation* draglocation = [[[CLLocation alloc] initWithLatitude:droppedAt.latitude longitude:droppedAt.longitude] autorelease];



    }
}
于 2013-01-30T12:54:56.660 に答える
1

突然、アノテーションのsetCoordinatesメソッドを実装し、アノテーションの座標が変更されたときにcallBAckを作成します

編集:移動中は注釈のdragStateプロパティに注意してください。

于 2012-05-24T08:34:36.960 に答える
1

あなたはこれをしたくないのではありませんか:

アノテーションデリゲートを設定してから

- (void)setCoordinate:(CLLocationCoordinate2D)newCoordinate {
   coordinate = newCoordinate; 
   if (self.delegate respondsToSelector:@selector(mapAnnotationCoordinateHaveChanged))
    [self.delegate performSelector(@selector(mapAnnotationCoordinateHaveChanged))];
}

あなたの代理人で:

- (void) mapAnnotationCoordinateHaveChanged{
 //some coordinates update code
}
于 2012-05-24T10:11:04.003 に答える
0

Swift では、私の問題は、coordinate属性がletサブクラスの a でvarあり、MKAnnotation.

class MyAnnotation  : NSObject, MKAnnotation {

    let title       : String?
    let subtitle    : String?
    var coordinate  : CLLocationCoordinate2D

    init(title: String, subtitle: String, coordinate: CLLocationCoordinate2D) {
        self.title = title
        self.subtitle = subtitle
        self.coordinate = coordinate
        super.init()
    }
}
于 2016-10-13T22:18:28.090 に答える
0

Swift 2.2、Xcode 7.3.1 の例:

  • MKAnnotation プロトコルを採用したカスタム クラスを作成する

    (注: Swift には KVO 準拠のプロパティの自動通知がないため、独自のプロパティを追加しました - これは必須ではありません):

    import MapKit
    
    class MyAnnotation: NSObject, MKAnnotation {
    
        // MARK: - Required KVO-compliant Property
        var coordinate: CLLocationCoordinate2D {
            willSet(newCoordinate) {
                let notification = NSNotification(name: "VTAnnotationWillSet", object: nil)
                NSNotificationCenter.defaultCenter().postNotification(notification)
            }
    
            didSet {
                let notification = NSNotification(name: "VTAnnotationDidSet", object: nil)
                NSNotificationCenter.defaultCenter().postNotification(notification)
            }
        }
    
    
        // MARK: - Required Initializer
        init(coordinate: CLLocationCoordinate2D) {
            self.coordinate = coordinate
        }
    }
    
  • ピン ビューをドラッグ可能として宣言します。

    // MARK: - MKMapViewDelegate Actions
    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    
        let pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: pinViewReuseIdentifier)
        pinView.draggable = true
    
        return pinView
    }
    

ピンがドラッグされると、delegate メソッドが呼び出されます。

func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView)

ここで、新しい終了座標を取得できます。


userInfoまたは、カスタム アノテーションのcoordinateプロパティの通知用の辞書を作成することもできます。オブザーバーは、通知の受信時に値を取得できます。

于 2016-08-08T22:45:44.027 に答える