2012-05-24 83 views
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:

Annotation.h

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

Annotation.m

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

編輯3: 在以往任何時候我拖到引腳上, NSLog ou放下的時間總是一樣的。

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

@rckoenes THX您的通知。 –

回答

38

首先,你必須創建一個自定義的註釋是這樣的:

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

你在哪裏設置了新座標? –

+1

這就是你實現'setCoordinate'方法的原因。從Apple的文檔 - > _Annotations支持拖動應實現此方法來更新註釋的位置._ – Alladinian

+0

我更新我的帖子中的座標(只是編輯它)。但似乎不起作用。 –

1

出於藍,實現您的註釋的方法setCoordinates,使一個回調,當你註釋座標已經改變

編輯:當心你的註釋,而其運動的dragState財產。

+0

Thx爲您的答案。 Plz檢查我的帖子的更新。 –

1

不要你wan't做到這一點:

設置註釋委託, 然後

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

在您的委託:

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

感謝您的完整解決方案 –

0

舉例雨燕2.2時,Xcode 7.3.1:

  • 創建自定義類採用MKAnnotation協議

    (注:斯威夫特不有自動通知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 
    } 
    

現在,銷拖動時,它調用的委託方法:

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

在這裏,你可以檢索新的結束座標。


或者,你可以創建一個userInfo字典自定義註釋的coordinate屬性的通知。觀察員可以在收到通知後檢索這些值。

0

在斯威夫特我的問題是,coordinate屬性是我的子類let而不是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() 
    } 
}