2012-05-24 10 views
13

Znacznik przypisu jest przeciągalny, ale nie można go upuścić w miejsce docelowe. Problem polega na tym, w jaki sposób mogę uzyskać współrzędną docelowej lokalizacji, w której upuszczam pinezkę adnotacji? Czy istnieje jakaś metoda?Przeciągnij znacznik adnotacji na widok mapy

Edit 1:

Centrum sworznia adnotacji nigdy nie wydaje zmienione, tam gdzie kiedyś wpadnę kołek.

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

} 
} 

Edycja 2:

Annotation.h

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

Annotation.m

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

Edycja 3: gdziekolwiek przeciągnąć z bolcami, NSLog ou t putted the droppedAt jest zawsze taki sam.

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

Edycja 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; 
} 
+0

@rckoenes thx za powiadomienie. –

Odpowiedz

38

Najpierw trzeba utworzyć niestandardowe adnotacji takiego:

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 

Po tym, po prostu trzeba użyj swojej adnotacji w ten sposób:

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

Należy również zwrócić widok dotyczący adnotacji.Można to zrobić tak:

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

Potem, kiedy już przyjęła protokół w kontrolerze MKMapViewDelegate, chwycić współrzędne kołkiem po przeciągnięciu go tak:

- (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); 
    } 
} 
+0

gdzie ustawiłeś nową współrzędną? –

+1

Dlatego wdrażasz metodę 'setCoordinate'. Z dokumentacji firmy Apple -> _Annotacje obsługujące przeciąganie powinny zaimplementować tę metodę, aby zaktualizować położenie adnotacji._ – Alladinian

+0

Aktualizuję współrzędne, tak jak w moim poście (po prostu edytuj). Ale wydaje się nie działać. –

1

Out of the blue, wdrożyć metodę setCoordinates swojej adnotacji i dokonać oddzwonienia, gdy współrzędne adnotacji zmieniły

edit: uwaga na własność dragState swojej adnotacji podczas gdy jego ruchu.

+0

Thx za odpowiedź. Plz sprawdź aktualizację mojego postu. –

1

Nie wan't aby to zrobić:

Ustawianie delegata adnotacji następnie

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

W swoim delegatem:

- (void) mapAnnotationCoordinateHaveChanged{ 
//some coordinates update code 
} 
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]; 



    } 
} 
+0

Dzięki za takie kompletne rozwiązanie –

0

przykładu dla Swift 2.2, Xcode 7.3.1:

  • tworzyć niestandardowe klasy przyjęcie protokołu MKAnnotation

    (uwaga: nie Swift mieć automatyczne powiadomienie o właściwościach zgodnych z KVO, więc dodałem własne - nie jest to wymagane):

    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 
        } 
    } 
    
  • Declare pin widok jak przeciągane:

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

Teraz, gdy trzpień jest przeciągany, to wywołuje metodę Delegat:

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

Tutaj można pobierz nowe współrzędne końcowe.


Alternatywnie, można utworzyć słownik userInfo powiadomień niestandardowego adnotacji za coordinate zakwaterowania. Obserwatorzy mogą następnie pobrać wartości po otrzymaniu powiadomienia.

0

w Swift moim problemem było to, że atrybut coordinate był let w moim podklasy, a nie var jak to jest w 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() 
    } 
}